Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the cropped version of an image using ALAsset?

Tags:

ios

iphone

I'm trying to get the cropped version of an image that's pulled using ALAsset. Specifically, I'm selecting items from the user's Photo Library and then uploading them. The issue is that in the library thumbnail view, iOS is showing us the cropped version. When you select that thumbnail and pull that image's asset using ALAsset, I get the full resolution version. I did some research and couldn't find anything that helps in getting a second coordinate system of where the cropping happens.

To test it, you need iOS5 to edit the image in your library. Select an image in your image library, select "Edit", and crop the image. When you get the ALAsset you'll get the full image, and if you sync using iPhoto, iPhoto also pulls the full image. Also, you can re-edit the image and undo your crop.

This is how I'm getting the image:

UIImage *tmpImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];

That gives me the full resolution image, obviously. There's a fullScreenImage flag which scales the full resolution image to the size of the screen. That's not what I want.

The ALAssetRepresenation class has a scale field, but that's a float value, which is also what I don't want.

If anyone can tell me where this cropped coordinate system can be found, I'd appreciate it.

like image 270
Michael Gaines Avatar asked Jul 19 '12 20:07

Michael Gaines


People also ask

How is an image cropped?

To “crop” an image is to remove or adjust the outside edges of an image (typically a photo) to improve framing or composition, draw a viewer's eye to the image subject, or change the size or aspect ratio. In other words, image cropping is the act of improving a photo or image by removing the unnecessary parts.

How do I crop a little part of a picture?

Click the picture. Click Picture Tools > Format, and in the Size group, click the arrow under Crop. From the menu that appears, select Aspect Ratio, then click the ratio that you want. A crop rectangle appears, showing you how the picture will appear when cropped to the selected aspect ratio.


2 Answers

Your Options:

Option 1 (ALAssetLibrary)

Use the - (CGImageRef)fullScreenImage method of AlAssetRepresentation.

Pros:

  • All the hard work is done for you, you get an image that looks just like the one in the Photos app. This includes cropping, and other changes. Easy.

Cons:

  • The resolution is "screen size", only as big as the device you are using, not the full possible resolution of the cropped image. If this doesn't concern you, then this is the perfect option.

Option 2 (ALAssetLibrary)

Extract the cropping data using the AdjustmentXMP key in the image's metadata (what @tom is referring to). Apply the crop.

Benefit:

  • It is possible to get a cropped image at the best possible resolution

Cons

  • You only get the cropping edits, not any other adjustments (like red-eye)
  • Who knows what Apple will support in the future in "Edit" mode, you may have to apply more edits in the future.
  • It's complicated, you first have to parse the XML data to read the crop rectangle, crop the unrotated image, and then apply the rotation.

Option 3 (Wishful Thinking)

Beg Apple to include a method like fullResolutionEditedImage which gives you the best possible quality photo, with all edits applied.

Pros:

  • Everything magically solved.

Cons:

  • Apple may never add this method.

Option 4 (UIImagePickerController)

This option only applies if you are using the image picker, you can't use it directly with the asset library

In the NSDictionary returned by -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

You can extract the full sized, adjusted image from the UIImagePickerControllerOriginalImage key. Save this image somewhere. Then, instead of retrieving the image from the asset library, load the copy you made.

Pros:

  • You get the full size image, with adjustments
  • This is the only option Apple gives us for getting the full size image with all adjustments (like red-eye, etc), and not just the crop. This is particularly important in iOS 7 with the introduction of filters that can drastically alter the image.

Cons:

  • Can only be used with the image picker (not ALAssetRepresentation)
  • You must keep around a full-sized copy of the image. Depending on the number of such images, the disk usage by your app could grow substantially.

Update for iOS 7: you may wish to consider Option 4, or Option 1, as iOS 7 supports many operations now like filters, and your users will probably notice if they are missing. These two options support filters (and other edits), with Option 4 giving you a higher resolution result.

like image 115
William Denniss Avatar answered Sep 16 '22 22:09

William Denniss


When a photo has been cropped with the iOS Photos App, the cropping coordinates can be found in the ALAssetRepresentation's metadata dictionary. fullResolutionImage will give you the uncropped photo, you have to perform the cropping yourself.

The AdjustmentXMP metadata contains not only the cropping coordinates but also indicates if auto-enhance or remove-red-eyes has been applied.

As of iOS 6.0 CIFilter provides filterArrayFromSerializedXMP:inputImageExtent:error: Probably you can use the ALAssetRepresentation's AdjustmentXMP metadata here and apply the CIFilter onto the ALAssetRepresentation's fullResolutionImage to recreate the modified image.

Be aware that the iOS Photos App handles JPG and RAW images differently. For JPG images a new ALAsset with the XMP metadata is stored in the Camera Roll. For RAW images an ALAssetRepresentation is added to the original ALAsset. I'm not sure if this additional ALAssetRepresentation is the modified image and if it has the AdjustmentXMP metadata. In addition to JPG and RAW images you should also test the behaviour for RAW+JPG images.

like image 21
tom Avatar answered Sep 18 '22 22:09

tom