【问题标题】:Displaying image from asset library显示资产库中的图像
【发布时间】:2014-07-27 02:40:10
【问题描述】:

我正在尝试获取使用 imagePickerControl 选择的图像并使用提供的 URL,将其打开并显示在 imageview 上。如果我使用 UIImagePickerControllerOriginalImage 来设置我的图像,它工作正常,但我不想将图像存储在我的数据库中,我只想存储 URL。在我将它进一步扩展到我的代码之前,我想确保它能够工作。 Bellow 是从 PickerController 返回所选图像并尝试显示图像的代码。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    self.imageURL=[info objectForKey:UIImagePickerControllerReferenceURL];
    CCLog(@"Image =%@",[info objectForKey:UIImagePickerControllerReferenceURL]);
//    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

    switch(status){
        case ALAuthorizationStatusDenied: {
            CCLog(@"not authorized");
            break;
        }
        case ALAuthorizationStatusRestricted: {
            CCLog(@"Restricted");
            break;
        }
        case ALAuthorizationStatusNotDetermined: {
            CCLog(@"Undetermined");
            break;
        }
        case ALAuthorizationStatusAuthorized: {
            CCLog(@"Authorized");
            CCLog(@"self.imageURL=%@",self.imageURL);
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            __block UIImage *returnValue = nil;
            [library assetForURL:self.imageURL resultBlock:^(ALAsset *asset) {
                returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
                } failureBlock:^(NSError *error) {
                    NSLog(@"error : %@", error);
            }];
            [self.imageDisplay setImage:returnValue];
            [self.imageDisplay setNeedsDisplay];
            break;
        }
        default: {
            CCLog(@"Unknown hit default");
            break;
        }

    }

    // You have the image. You can use this to present the image in the next view like you require in `#3`.

}

当它运行时,当我跟踪它并且没有显示图像时,returnValue 会显示为 nil。我从https://stackoverflow.com/a/13276649/3723298得到代码

谢谢

【问题讨论】:

    标签: objective-c uiimage alassetlibrary


    【解决方案1】:

    assetForURL:resultBlock:failureBlock: 是异步的。你想要这样的东西:

        case ALAuthorizationStatusAuthorized: {
            CCLog(@"Authorized");
            CCLog(@"self.imageURL=%@",self.imageURL);
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            [library assetForURL:self.imageURL resultBlock:^(ALAsset *asset) {
                UIImage *returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.imageDisplay setImage:returnValue];
                    [self.imageDisplay setNeedsDisplay];
                });
            } failureBlock:^(NSError *error) {
                NSLog(@"error : %@", error);
            }];
            break;
        }
    

    顺便说一句 - 这有什么意义?您可以更直接地从图像选择器委托方法中获取图像。无需再次从资源库中获取图片。

    只要做:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        self.imageDisplay.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    

    【讨论】:

    • 请说明如何从委托方法中获取它。我在其他帖子中没有看到任何提及。
    • 哦,是的,我明白了。您帮助的代码将在不同场景的另一个类中使用。我只是在这里得到了 URL 以确保它在我在另一个类中重新实现它并使事情变得更复杂之前工作。顺便说一句,你的答案很完美。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2016-02-10
    • 2018-01-16
    • 2022-08-04
    相关资源
    最近更新 更多