Last active
December 16, 2015 13:09
-
-
Save klazuka/5439717 to your computer and use it in GitHub Desktop.
Workaround Apple's limited API for obtaining the "full resolution" cropped & rotated photo. Uses CoreImage on iOS 6+. Punts on iOS <6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static CIImage* ApplyFiltersToImage(NSArray *filters, CIImage *inputImage) | |
{ | |
CIImage *img = inputImage; | |
for (CIFilter *filter in filters) | |
{ | |
[filter setValue:img forKey:kCIInputImageKey]; | |
img = filter.outputImage; | |
} | |
return img; | |
} | |
static UIImage *EditedOrOriginalImageFromAssetRepresentation(ALAssetRepresentation *ar) | |
{ | |
UIImage *originalImage = [UIImage imageWithCGImage:ar.fullResolutionImage scale:ar.scale orientation:ar.orientation]; | |
if ( ! [[CIFilter class] respondsToSelector:@selector(filterArrayFromSerializedXMP:inputImageExtent:error:)]) | |
{ | |
// No CoreImage available, so we will just return the original image | |
return originalImage; | |
} | |
// most common case: if the image has not been edited, just return the original image | |
NSData *xmpData = [ar.metadata[@"AdjustmentXMP"] dataUsingEncoding:NSUTF8StringEncoding]; | |
if (!xmpData) | |
return originalImage; | |
// The image has been edited: use CoreImage to apply the crop & rotate metadata on the full resolution image | |
NSError *xmpError; | |
CGRect imageExtent = (CGRect){CGPointZero, ar.dimensions}; | |
NSArray *filters = [CIFilter filterArrayFromSerializedXMP:xmpData inputImageExtent:imageExtent error:&xmpError]; | |
if (!filters) | |
{ | |
NSLog(@"WARNING: failed to apply AdjustmentXMP. Error %@", xmpError); | |
return originalImage; | |
} | |
CIImage *inputImage = [CIImage imageWithCGImage:ar.fullResolutionImage]; | |
CIImage *outputImage = ApplyFiltersToImage(filters, inputImage); | |
return [UIImage imageWithCIImage:outputImage scale:ar.scale orientation:ar.orientation]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment