ALAsset defaultRepresentation fullResolutionImage

时间:2013-09-24 12:36:25

标签: ios objective-c uiimage ios7 alassetslibrary

我遇到了新的iOS 7照片滤镜功能问题。

我的应用程序中有一个photolibrary。当我在UICollectionView中显示照片的缩略图时,我会收到已应用过滤器和裁剪的图像。有两种方法可以返回“随时可用”的图像:

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

相反,当我想分享全尺寸图像时,我会收到没有任何过滤器的未更改照片:

是否可以使用适当的过滤器获取完整尺寸的图像?

1 个答案:

答案 0 :(得分:27)

到目前为止,我只想出一种方法来获得我想要的东西。所有资产都通过密钥@"AdjustmentXMP"在元数据字典中存储其修改(如过滤器,作物等)信息。我们能够解释这些数据并将所有过滤器应用到fullResolutionImage,就像这里SO answer.这是我的完整解决方案:

...
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]];