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) {
...
}];
But when I try approach with url it fails:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:contentEditingOutput.renderedContentURL];
} completionHandler:^(BOOL success, NSError *error) {
...
}];
Error:
Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)"
UPDATE:
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) {
...
}];
The method works for a video (creationRequestForAssetFromVideoAtFileURL:
), but not for an image. What went wrong?
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.renderedContentURL
The solution is to convert the image with function
When passing the metadata, I also had the issue consistently showing whenever the Orientation (inside the image metadata) was anything other than
CGImagePropertyOrientationUp
.This is also stated inside the
renderedContentURL
documentation:For images, the following metadata keys need to be updated (while the image data is also rotated):
•
kCGImagePropertyTIFFDictionary
\kCGImagePropertyTIFFOrientation
•
kCGImagePropertyOrientation
• possibly,
kCGImagePropertyIPTCImageOrientation