2

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 .

Shishir Shetty
  • 2,021
  • 3
  • 20
  • 35

2 Answers2

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

Shishir Shetty
  • 2,021
  • 3
  • 20
  • 35
1

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.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I got this in console no image data found for URL assets-library://asset/asset.JPG?id=79450962-C0FD-484C-808B-83E045F40972&ext=JPG But I selected an image from the Photo Gallery. What seems to be the problem? – Shishir Shetty Dec 01 '11 at 16:12
  • I'm glad you were able to figure out where the problem was happening. Now it looks like you need to figure out how to convert that reference URL into something you can actually load into an NSData object. Maybe there is another key in the `info` NSDictionary that could help you? :-) – Michael Dautermann Dec 01 '11 at 16:16
  • BUt the URL seems to be a valid one :( Then why there is no image data? – Shishir Shetty Dec 01 '11 at 16:22
  • You have to access that type of image data using `ALAssetsLibrary`. Look for sample code that involves `ALAssetsLibraryAssetForURLResultBlock`. – Michael Dautermann Dec 01 '11 at 16:25
  • Sorry @Michael Dautermann didnt found anything helpfull on net. I am new to iphone. Can you please suggest me something. – Shishir Shetty Dec 01 '11 at 17:01
  • [Look at the answer for this question](http://stackoverflow.com/questions/3837115/display-image-from-url-retrieved-from-alasset-in-iphone). – Michael Dautermann Dec 01 '11 at 17:04