ALAssetsLibrary返回没有外部裁剪或其他过滤器的图像

时间:2014-03-16 05:26:41

标签: ios7 uiimagepickercontroller alassetslibrary

我在iOS7上遇到一个奇怪的问题。

用户可以在“照片”应用中打开图片并进行裁剪。

在我的应用程序中,当我尝试获取图像时,它会在没有裁剪的情况下返回。其他编辑(例如旋转)将被保留。

我在没有编辑功能的情况下呈现图像选择器。

我的代码:

ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:[info valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    Byte *buffer = (Byte*)malloc(rep.size);
    NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
    NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want

    editedImage = [UIImage imageWithData:data];

} failureBlock:^(NSError *err) {
    NSLog(@"Error: %@",[err localizedDescription]);
}];

如果我使用

UIImage *image = info[UIImagePickerControllerOriginalImage];
content = UIImageJPEGRepresentation(image, 1.0);

正确裁剪图像。

这是iOS7中的错误吗?

1 个答案:

答案 0 :(得分:1)

事实证明,裁剪实际上是保存在元数据中的。

这是我最终做的事情:

ALAssetRepresentation *rep = [asset defaultRepresentation];

NSString *xmpString = rep.metadata[@"AdjustmentXMP"];

if(xmpString)
{
    NSData *xmpData = [xmpString dataUsingEncoding:NSUTF8StringEncoding];

    CIImage *image = [CIImage imageWithCGImage:rep.fullResolutionImage];

    NSError *error = nil;
    NSArray *filterArray = [CIFilter filterArrayFromSerializedXMP:xmpData
                                                 inputImageExtent:image.extent
                                                            error:&error];

    if (error)
    {
        DM_FLOG(@"ERROR:: Error during CIFilter creation: %@", [error localizedDescription]);
    }

    for (CIFilter *filter in filterArray) {
        [filter setValue:image forKey:kCIInputImageKey];
        image = [filter outputImage];
    }

    CIContext* context = [CIContext contextWithOptions:nil];
    CGImageRef cgImage = [context createCGImage:image fromRect:[image extent]];
    context = nil;
    image = nil;

    contentType = @"image/jpeg";
    content = UIImageJPEGRepresentation([UIImage imageWithCGImage:cgImage], 1.0);

    CGImageRelease(cgImage);
}
else
{
    Byte *buffer = (Byte*)malloc(rep.size);
    NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
    content = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
}