Search code examples
iosios8ios8-extension

PHAdjustmentData is nil in "startContentEditingWithInput"


I'm working on a photo-editing extension app, and I want it to be able to save the information about the changes made into the PHAdjustmentData, so that user can modify those changes later. I save all the required data into PHAdjustmentData, however, next time I edit the image - PHAdjustmentData is nil. Here is a sample code:

@property (strong, nonatomic) PHContentEditingOutput *output;

- (void)startContentEditingWithInput:(PHContentEditingInput *)contentEditingInput placeholderImage:(UIImage *)placeholderImage
{
    self.output = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput];

    // here contentEditingInput.adjustmentData is always nil
}

- (void)finishContentEditingWithCompletionHandler:(void (^)(PHContentEditingOutput *))completionHandler
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"value1" forKey:@"key1"];
    [dict setObject:@"value2" forKey:@"key2"];
    NSData *adjData = [NSKeyedArchiver archivedDataWithRootObject:dict];
    PHAdjustmentData *phAdjData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"ident" formatVersion:@"1.0" data:adjData];
    self.output.adjustmentData = phAdjData;

    NSData *data = UIImageJPEGRepresentation(result, 1.0);
    [data writeToURL:self.output.renderedContentURL options:NSDataWritingAtomic error:nil];

    completionHandler(self.output);
}

How do I save the adjustment data properly, so that I can access it next time user edits the image? Thanks!


Solution

  • PhotoKit gives you adjustment data in startContentEditingWithInput only if you've agreed to handle that data. You agree by implementing canHandleAdjustmentData to check the format identifier and version of the adjustment and then return true for formats you can handle.

    If you return false from canHandleAdjustmentData, then you'll get a nil adjustment data in startContentEditingWithInput. Not only that, you'll also get a pre-edited version of the photo image, so you can't work off of how it looked before the last edit.