Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access cropped or edited images using ALAssetsLibrary

I have an ipad app which takes images from Photos application using ALAssetsLibrary and then uploads them to web server. So far everything is working fine.

Now if i go to the photos application and edit an image (say crop) and upload, the original un-cropped is getting uploaded. I am accessing/saving a the photo using the asset url.

Ex:

createPhoto: 
assetUrl: assets-library://asset/asset.JPG?id=542F09CA-00E1-412E-A7E6-0C222E3F8FFB&ext=JPG, 
UTIs: (
    "public.jpeg"
), 
UTI:public.jpeg

This is not a ALAssetsLibrary cache issue becuase, i have tried editing, kill app, start, upload which still uploads old image.

Question:

  1. How do i know if a edited (cropped/red eye/enhanced) version of the photo is available?
  2. How do i access the edited version of the photo?
like image 487
mithuntnt Avatar asked Jan 07 '12 15:01

mithuntnt


2 Answers

I recognized the same behaviour and did some testing a while ago. Let me share my results with you:

  1. The edited image you only get through the fullScreenImage method. This is also mentioned in Apple's documentation: "In iOS 5 and later, this method returns a fully cropped, rotated, and adjusted image—exactly as a user would see in Photos or in the image picker."

  2. The fullResolutionImage and getBytes method return the unedited image. However, the editing parameters like crop are saved in the image metadata. This metadata info however is only interpreted by Apple apps like Aperture and iPhoto.

  3. You can find out, if an image had been edited, by inspecting he image metadata. Compare the metadata of an unedited and edited image and look for differences in the metadata fields.

like image 148
holtmann Avatar answered Nov 17 '22 01:11

holtmann


-(UIImage*)fullScreenImage:(ALAsset *)imageAsset{
    ALAssetRepresentation *assetRepresentation = [imageAsset defaultRepresentation];
    CGImageRef fullResImage = [assetRepresentation fullResolutionImage];
    NSString *adjustment = [[assetRepresentation metadata] objectForKey:@"AdjustmentXMP"];
    if (adjustment) {
        NSData *xmpData = [adjustment dataUsingEncoding:NSUTF8StringEncoding];
        CIImage *image = [CIImage imageWithCGImage:fullResImage];

        NSError *error = nil;
        NSArray *filterArray = [CIFilter filterArrayFromSerializedXMP:xmpData
                                                     inputImageExtent:image.extent
                                                                error:&error];
        CIContext *context = [CIContext contextWithOptions:nil];
        if (filterArray && !error) {
            for (CIFilter *filter in filterArray) {
                [filter setValue:image forKey:kCIInputImageKey];
                image = [filter outputImage];
            }
            fullResImage = [context createCGImage:image fromRect:[image extent]];
        }
    }
    UIImage *result = [UIImage imageWithCGImage:fullResImage
                                          scale:[assetRepresentation scale]
                                    orientation:(UIImageOrientation)[assetRepresentation orientation]];
    return result;
}

In above function pass ALAsset value then you the edited image ...:)

Above code for getting edited image from Meta data

If u go with full screen image then also u will get the edited image

ALAssetRepresentation *representation = [asset defaultRepresentation];
 CGImageRef iref = [representation fullScreenImage];
 UIImage *editedImage = [UIImage imageWithCGImage:iref];
like image 29
Adarsh G J Avatar answered Nov 17 '22 00:11

Adarsh G J