简体   繁体   English

使用UIImagePickerControllerReferenceURL在UIImageView上设置图像

[英]Set Image on UIImageView using UIImagePickerControllerReferenceURL

Is it possible to set the image on UIImageView using UIImagePickerControllerReferenceURL . 是否可以使用UIImagePickerControllerReferenceURL在UIImageView上设置图像。 The traditional pattern is using [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 传统模式使用[info objectForKey:@“ UIImagePickerControllerOriginalImage”];

I tried this:

theImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[info objectForKey:UIImagePickerControllerReferenceURL]]]; theImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[info objectForKey:UIImagePickerControllerReferenceURL]]]];

But no image is set on UIImageView. 但是,UIImageView上没有设置图像。

Please Help . 请帮忙 。

NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library assetForURL:referenceURL resultBlock:^(ALAsset *asset)
         {
           UIImage  *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
             theImageView.image = copyOfOriginalImage;
         }
                failureBlock:^(NSError *error)
         {
             // error handling
         }];
        [library release];

This code did the trick. 这段代码成功了。 :) :)

Instead of embedding four or five functions on one line, you need to do more error handling in your code. 您需要在代码中执行更多错误处理,而不是在一行上嵌入四个或五个函数。

Consider this instead: 考虑一下这个:

NSURL * referenceURL = [info objectForKey: UIImagePickerControllerReferenceURL];
if(referenceURL == NULL)
{
    NSLog( @"reference URL is null");
} else {
    NSData * imageData = [NSData dataWithContentsOfURL: referenceURL];
    if(imageData == NULL)
    {
        NSLog( @"no image data found for URL %@", [referenceURL absoluteString] );
    } else {
        UIImage * theActualImage = [UIImage imageWithData: imageData];
        if(theActualImage == NULL)
        {
            NSLog( @"the data at URL %@ is not an image", [referenceURL absoluteString] );
        } else {
            if(theImageView == NULL)
            {
                NSLog( @"I forgot to set theImageView" );
            } else {
                theImageView.image = theActualImage;
            }
        }
    }
}

I hope this info helps you out. 希望此信息对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM