Get the “retouched” image from ALAssetRepresentati

2019-02-18 11:34发布

Given an ALAssetRepresentation, is it possible to get its full resolution retouched image?

If I use the method fullResolutionImage I get the full resolution image, but unadjusted in any way.

If I use the method fullScreenImageI get the retouched image, but reduced to an appropriate for displaying full screen.

1条回答
何必那么认真
2楼-- · 2019-02-18 12:14

It’s not that easy, but you can. Notice that this will also apply any cropping done by the user in the Photos.app:

ALAssetRepresentation *representation = asset.defaultRepresentation;
CGImageRef fullResolutionImage = CGImageRetain(representation.fullResolutionImage);
// AdjustmentXMP constains the Extensible Metadata Platform XML of the photo
// This XML describe the transformation done to the image.
// http://en.wikipedia.org/wiki/Extensible_Metadata_Platform
// Have in mind that the key is not exactly documented.
NSString *adjustmentXMP = [representation.metadata objectForKey:@"AdjustmentXMP"];

NSData *adjustmentXMPData = [adjustmentXMP dataUsingEncoding:NSUTF8StringEncoding];
NSError *__autoreleasing error = nil;
CGRect extend = CGRectZero;
extend.size = representation.dimensions;
NSArray *filters = [CIFilter filterArrayFromSerializedXMP:adjustmentXMPData inputImageExtent:extend error:&error];
if (filters)
{
  CIImage *image = [CIImage imageWithCGImage:fullResolutionImage];
  CIContext *context = [CIContext contextWithOptions:nil];
  for (CIFilter *filter in filters)
  {
    [filter setValue:image forKey:kCIInputImageKey];
    image = [filter outputImage];
  }

  CGImageRelease(fullResolutionImage);
  fullResolutionImage = [context createCGImage:image fromRect:image.extent];
}

// At this moment fullResolutionImage will be the filtered image, or the full
// resolution one if no filters were applied.
// You will need to CGImageRelease fullResolutionImage after you have finished
// working with it.
查看更多
登录 后发表回答