2
votes

Is it possible to set the image on UIImageView using UIImagePickerControllerReferenceURL . The traditional pattern is using [info objectForKey:@"UIImagePickerControllerOriginalImage"];

I tried this:

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

But no image is set on UIImageView.

Please Help .

2

2 Answers

5
votes
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. :)

1
votes

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.