将图片保存到网址时,PHPhotoLibrary错误

Tom*_*Bąk 5 url image ios photokit

我在提供的url上创建了一个图片PHContentEditingOutput。当我UIImage像这样加载数据并将其保存时,它可以工作。

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

但是当我尝试使用url方法失败时:

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

错误:

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

更新:

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

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

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

Tom*_*Bąk 5

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

renderingContentURL

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

解决方案是使用功能转换图像

NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
Run Code Online (Sandbox Code Playgroud)