1
votes

I have two view controllers, in one I pick the image from the photo library and assign it to an image property. With this picker:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo

But instead I would like to just get the address/data of the image from the picker in the first controller, push the next view controller and then open the image with that address there. So then I do the following:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

When I print info of the photo it looks something like this:

assets-library://asset/asset.JPG?id=52219875-C221-4F60-B9D4-984AAADB6E63&ext=JPG

The problem is that UIImage has methods initWithData or imageWithData but these accept NSData and not NSDictionary.

How can I open an image from a photo library using its data?

3

3 Answers

2
votes

This method:

- (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)img 
                  editingInfo:(NSDictionary *)editInfo

shouldn't be used, it's been deprecated since ios3

This method:

  - (void)imagePickerController:(UIImagePickerController *)picker 
  didFinishPickingMediaWithInfo:(NSDictionary *)info

returns a dictionary, as you report.

The image pointer is one of the objects in this dictionary. You obtain it as such:

  UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
1
votes

Here's Apple's documentation for that method.

Basically there are a few keys in that dictionary, listen here:

NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;

You probably want UIImagePickerControllerEditedImage. So this is what you're looking for:

UIImage *theImage = [info valueForKey:UIImagePickerControllerEditedImage];

Of course you can get other info from the image with the other keys in the dictionary too.

0
votes

The dictionary provides the URL for the image. So load the data by

NSData *imageData = [NSData dataWithContentsOfURL:[info valueForKey: UIImagePickerControllerReferenceURL]];

and use this data for the NSImage initWithData method.