我的应用程序拍照,将它们保存到相机胶卷,并允许修改 EXIF 元数据。
我使用 AssetsLibrary 来保存和修改照片(我不能使用新的 PhotoKit api,因为我需要修改底层的 EXIF,而且它是一个遗留应用程序,需要大量重构才能更改它)。
我正在使用 Xcode 6.3.1,带有 iOS 8.3 SDK,部署目标为 6.1。
在 iOS 8.2 中,这一切都奏效了。
在 iOS 8.3 中,元数据的编辑失败。
该应用程序在隐私设置中具有访问照片的权限。
当用户修改照片并且应用程序尝试重写照片时,iOS 8.3 现在会显示“允许应用程序修改此照片”对话框。此对话框未出现在 iOS 8.2 中。如果我点击“修改”,照片会被保存,但元数据会被擦除。setImageData 也没有返回错误,我检查照片是否可编辑。
这是代码:
-(void)savePhoto:(ALAsset*)asset
{
ALAssetRepresentation* rep = [asset defaultRepresentation];
CGImageRef imageRef = [rep fullResolutionImage];
UIImage *image = [UIImage imageWithCGImage:imageRef];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
if ([asset isEditable])
{
[asset setImageData:imageData metadata:self.getMetadataDictionary completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error!=nil)
[self showErrorDialog:error title:@"Error saving photo" ];
[self closeSaveDialog];
}];
}
else
{
[self closeSaveDialog];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error saving photo" message:@"Photo is not editable" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
调试时,我注意到我的代码中的一个错误,即 UIImageJPEGRepresentation 将照片大小增加了 4 倍,因为它正在重新采样照片,所以我更改了代码以获取原始图像字节,并仅重写元数据。但有趣的是,我得到了一个不同的错误,这次 setImageData 返回了这个错误。
描述:“用户拒绝访问”。
潜在错误:“ALAssetsLibraryErrorDomain”代码 -3311。
FailureReason:“用户拒绝应用程序访问他们的媒体”。
这很奇怪,因为应用程序创建了资产,并且可以访问相机胶卷。
同样,此代码适用于 iOS 8.2:
-(void)savePhoto:(ALAsset*)asset
{
ALAssetRepresentation* rep = [asset defaultRepresentation];
// New way handling updating photos, doesn't modify image data at all, only metadata
Byte *buffer = (Byte*)malloc((unsigned long)rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:(NSUInteger)rep.size error:nil];
NSData *imageData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
if ([asset isEditable])
{
[asset setImageData:imageData metadata:self.getMetadataDictionary completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error!=nil)
[self showErrorDialog:error title:@"Error saving photo" ];
[self closeSaveDialog];
}];
}
else
{
[self closeSaveDialog];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error saving photo" message:@"Photo is not editable" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
我向 Apple 提交了错误报告,但没有收到任何回复。
这类似于这个问题:setImageData 在 iOS 8.3 中失败
有谁知道解决这个问题?
谢谢,