Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALAsset defaultRepresentation fullResolutionImage

I have an issue with new iOS 7 photo filters feature.

I have a photolibrary in my app. While I showing photo's thumbnails in UICollectionView I receive images with filters and crops already applied. There are two methods that return "ready for use" images:

  • [asset thumbnail]
  • [[asset defaultRepresentation] fullScreenImage]

On the contrary, when I want to share fullsize image I receive unchanged photo without any filters:

  • [[asset defaultRepresentation] fullResolutionImage]
  • Read image data through getBytes:fromOffset:length:error:

Is it possible to get a fullsize image with filter appropriate applied?

like image 591
Wisors Avatar asked Sep 24 '13 12:09

Wisors


1 Answers

So far I figured out only one way to get what I want. All assets store their modification (like filters, crops and etc) info in the metadata dictionary by the key @"AdjustmentXMP". We're able to interpret this data and apply all filters to the fullResolutionImage like in this SO answer. Here is my complete solution:

...
ALAssetRepresentation *assetRepresentation = [asset 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]];
like image 105
Wisors Avatar answered Sep 29 '22 10:09

Wisors