18

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:

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

Wisors
  • 630
  • 6
  • 15
  • It's not clear what you are trying to do. Please restate the question so we can understand exactly what you expect to achieve. – RyanR Oct 15 '13 at 19:36
  • 1
    You have a spelling error in line 5 of your code: "if (adjusment)". Have you thought about posting your possible solution as answer? So we can vote it up. – Klaas Oct 19 '13 at 15:36

1 Answers1

27

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]];
Community
  • 1
  • 1
Wisors
  • 630
  • 6
  • 15
  • Thanks It also helped me :) – Eyal Oct 26 '13 at 09:43
  • 1
    With iOS 8, I can't find @"AdjustmentXMP" inside the metadata dictionary. – Andree Oct 15 '14 at 05:58
  • 1
    @Andree in iOS 8 getBytes:fromOffset:length:error: already give you an adjusted photo. No need to do something. – Wisors Oct 15 '14 at 08:38
  • 1
    @Wisors that's *kindof* true. But there's an Apple bug where the non-adjusted photo is returned if you grab it quickly enough after making edits. http://openradar.appspot.com/radar?id=6416588328665088 – paulrehkugler Dec 11 '14 at 22:39