显示资产库中的图像

时间:2014-07-27 02:40:10

标签: objective-c uiimage alassetlibrary

我尝试使用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

获得了代码

由于

1 个答案:

答案 0 :(得分:0)

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;
    }
BTW - 有什么意义呢?您可以直接从图像选择器委托方法获得图像。无需再从资产库中获取图像。

只是做:

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

    [self dismissViewControllerAnimated:YES completion:nil];
}
相关问题