ios中获编辑过的图片问题

在开发中有时需要获取经过编辑过的图片,比图裁减,滤镜等,那么该如何获取呢?下面提出几种方法拱参考。

1. 使用 AlAssetRepresentation的fullScreenImage方法,该方法最简单,获取的图片跟相册里面看到的图片一样,因为系统”相册”程序显示的图片是 fullScreenImage ,而不是fullResolutionImage ,但是该方法的问题是获得图片分辨率只有屏幕尺寸这么大,在碰到长图的时候就无能为力了,而如果我们使用fullResolutionImage 的话,会发现取的图片是原图,而非编辑过的。因此如果不考虑分辨率的问题,那么该方法是获取编辑过的图片的一个好的选择。  

2.   使用图片metadata字典中的AdjustmentXMP信息,所有的asset会把他们相关的修改信息(裁减,滤镜等)放在metadata字典中的AdjustmentXMP中,因此我们可以解析出其修改信息,并应用到fullResolutionImage上, 这样就能获得高分辨率且编辑过的图片,即便是长图也能应付。

        ALAssetRepresentation *representation = [asset defaultRepresentation];
        CGImageRef fullResImage = [representation fullResolutionImage];
        NSString *adjustment = [[representation 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 *adjustImage = [UIImage imageWithCGImage:fullResImage
                                                   scale:[representation scale]
                                             orientation:(UIImageOrientation)[representation orientation]];
posted @ 2014-09-26 14:49  hello_LJ  阅读(413)  评论(0编辑  收藏  举报