Hi everyone,
Summary:
I am trying to edit the metadata of the camera roll photo.
However, if you try to overwrite the photo after editing the metadata, always giving me the following error:
'Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn't be completed. (Cocoa error -1.)'
This occurs with photos such as landscape images (with orientation tag '6').
For portrait images (orientation tag is '1'), it is possible to overwrite and save photos correctly.
If you decode with UIImageJPEGRepresentation (image, 1.0), landscape images can be overwritten and saved.
But I do not want to reduce the quality of the image, so I am looking for another way.
Steps to Reproduce:
1. Save the picture of the camera roll under 'Documents / .
2. Edit the metadata of one photo. (From now on, under Documents /)
3. Get the asset "requestContentEditingInputWithOptions ()".
4. Acquire the URL of the photo of 'Step2', and acquire CGImageSourceRef with 'CGImageSourceCreateWithURL ()'
5. Get CGImageDestinationRef by specifying contentEditingOutput.renderedContentURL with
'CGImageDestinationCreateWithURL ()'.
6. Set CGImageSourceRef, CGImageDestinationRef, and metadata to CGImageDestinationAddImageFromSource.
7. Save CGImageDestinationRef using CGImageDestinationFinalize.
8. Create PHAadjustmentData. Set metadata to 'data'.
9. Set the AdjustmentData in setAdjustmentData
10. Set the set in changeRequestForAsset of PHPhotoLibrary. → 'PHAssetChangeRequest * request = [PHAssetChangeRequest
changeRequestForAsset: asset];'
11. 'request.contentEditingOutput = contentEditingOutput;'
12. 'completion' is an error. following error: 'Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn't be
completed. (Cocoa error -1.)'
following the code:
- (void)replaceMetadataIntoPhoto:(NSInteger)index metadata:(NSDictionary *)metadata
{
PHAsset *asset = _assetsList[index];
[asset requestContentEditingInputWithOptions:nil
completionHandler:^(PHContentEditingInput *_Nullable contentEditingInput, NSDictionary *_Nonnull info) {
NSURL *url = [contentEditingInput fullSizeImageURL];
CGImageSourceRef sourceImage = CGImageSourceCreateWithURL((__bridge CFURLRef)url, nil);
PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput];
CGImageDestinationRef outputDestination = CGImageDestinationCreateWithURL((__bridge CFURLRef)contentEditingOutput.renderedContentURL, CGImageSourceGetType(sourceImage), 1, NULL);
CGImageDestinationAddImageFromSource(outputDestination, sourceImage, 0, (__bridge CFDictionaryRef)metadata);
CGImageDestinationFinalize(outputDestination);
CFRelease(sourceImage);
CFRelease(outputDestination);
PHAdjustmentData *adjustmentData =
[[PHAdjustmentData alloc] initWithFormatIdentifier:@"hogehoge"
formatVersion:@"1.0"
data:[NSKeyedArchiver archivedDataWithRootObject:@{@"metadata": metadata}]];
[contentEditingOutput setAdjustmentData:adjustmentData];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset];
request.contentEditingOutput = contentEditingOutput;
} completionHandler:^(BOOL success, NSError *error) {
if (error) {
DBGLog(@"error=%@", error);
}
}];
Expected Results:
Ability to overwrite and save a landscape image with edited metadata on the camera roll.
Actual Results:
Fails in overwrite save.