Yaman Yaman - 1 year ago 64
iOS Question

ios 8 Photos framework - replace photos not working

I'm playing around the new iOS 8 Photos framework, trying to edit an existing photo in my library then replacing it but it's not replaced, even if I can see all the success log messages I've set.

Here's my code :

UIImage *output = [self.imageView.image appendDateWithLabel:label];
self.imageView.image = output;

// self.info was retrieve by the UIImagePickerController delegate method
NSURL *assetURL = self.info[UIImagePickerControllerReferenceURL];
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil];
PHAsset *asset = result.firstObject;
if ([asset canPerformEditOperation:PHAssetEditOperationContent])
[asset requestContentEditingInputWithOptions:nil completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {

PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput];
NSData *outputData = UIImagePNGRepresentation(output);
BOOL wrote = [outputData writeToURL:contentEditingOutput.renderedContentURL options:NSDataWritingAtomic error:nil];

if (wrote)
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset];
request.contentEditingOutput = contentEditingOutput;

} completionHandler:^(BOOL success, NSError *error) {
// console output : 1
NSLog(@"success : %@", @(success));
// console output : nil
NSLog(@"error : %@", error);

Instead of replacing, I've also tried to create a modified copy. This worked well, I could see the new modified photo in my library.

Someone already faced this issue ?

Answer Source

It seems that filling the adjustementData property of the PHContentEditingOutput object is mandatory in order to edit a photo.

PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"AdjustementDataIdentifier" formatVersion:@"1.0" data:nil]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download