在URL中保存图片时出现PHPhotoLibrary错误

我在PHContentEditingOutput提供的URL上创build一个图像。 当我加载数据到UIImage并保存这样 – 它的作品。

 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ NSData *data = [NSData dataWithContentsOfURL:contentEditingOutput.renderedContentURL] UIImage *image = [UIImage imageWithData:data]; [PHAssetChangeRequest creationRequestForAssetFromImage:image]; } completionHandler:^(BOOL success, NSError *error) { ... }]; 

但是,当我尝试与URL的方法失败:

 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:contentEditingOutput.renderedContentURL]; } completionHandler:^(BOOL success, NSError *error) { ... }]; 

错误:

错误域= NSCocoaErrorDomain代码= -1“操作无法完成。(Cocoa错误-1。)”

更新:

当我尝试保存修改时出现同样的错误。

 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset]; request.contentEditingOutput = contentEditingOutput; } completionHandler:^(BOOL success, NSError *error) { ... }]; 

该方法适用于video( creationRequestForAssetFromVideoAtFileURL: ,但不适用于图像。 什么地方出了错?

问题在于文件格式。 我试图编辑PNG屏幕截图,但renderingContentURL始终是tmp /文件名。 JPG 。 起初我以为这是一个错误,但根据文件,这是正确的行为。

renderedContentURL

阅读此属性以查找用于编辑已编辑资产内容的URL。 然后,如果要编辑照片资源,请将更改后的照片图像写入此URL的JPEG格式的文件。 如果编辑video资源,请将video导出到此URL的QuickTime(.mov)文件中。

解决scheme是将图像转换为函数

 NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);