在url上保存图像时出现PHPhotoLibrary错误 [英] PHPhotoLibrary error while saving image at url
问题描述
我在 PHContentEditingOutput
提供的网址上创建了一个图片。当我将数据加载到 UIImage
并保存它时 - 它可以工作。
I create an image at url provided by PHContentEditingOutput
. When I load data to UIImage
and save it like this - it works.
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
NSData *data = [NSData dataWithContentsOfURL:contentEditingOutput.renderedContentURL]
UIImage *image = [UIImage imageWithData:data];
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:^(BOOL success, NSError *error) {
...
}];
但是当我尝试使用url时,它会失败:
But when I try approach with url it fails:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:contentEditingOutput.renderedContentURL];
} completionHandler:^(BOOL success, NSError *error) {
...
}];
错误:
Error Domain = NSCocoaErrorDomain Code = -1无法完成操作。(Cocoa error -1。)
Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)"
更新:
尝试保存修改时出现同样的错误。
The same error when I try to save a modification.
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset];
request.contentEditingOutput = contentEditingOutput;
} completionHandler:^(BOOL success, NSError *error) {
...
}];
该方法适用于视频( creationRequestForAssetFromVideoAtFileURL:
),但不是图像。出了什么问题?
The method works for a video (creationRequestForAssetFromVideoAtFileURL:
), but not for an image. What went wrong?
推荐答案
问题在于文件格式。我正在尝试修改 PNG 屏幕截图,但 renderingContentURL
始终是 tmp / filename。 JPG 。起初我认为这是一个错误,但根据文档这是正确的行为。
The problem is in the file format. I was trying to edit PNG screenshot, but renderingContentURL
was always tmp/filename.JPG. At first I thought it was a bug, but according to the documentation this is correct behaviour.
阅读此属性以查找用于编写已编辑资产内容的URL。然后,如果编辑照片资产,请将更改后的照片图像写入此URL的JPEG格式文件。如果正在编辑视频资源,请将视频导出到此URL的QuickTime(.mov)文件。
Read this property to find a URL for writing edited asset content. Then, if editing a photo asset, write the altered photo image to a file in JPEG format at this URL. If editing a video asset, export the video to a QuickTime (.mov) file at this URL.
解决方案是转换具有功能的图像
The solution is to convert the image with function
NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
这篇关于在url上保存图像时出现PHPhotoLibrary错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!