3

I am making Photo Extension, but when I am trying to save changes(Done button Tapped).

Alert Message Says "Unable to Save Changes" - 'An Error occurred while saving. Please try again later.'

This is my code for finishContentEditingWithCompletionHandler:completionHandler

- (void)finishContentEditingWithCompletionHandler:(void (^)(PHContentEditingOutput *))completionHandler {
    // Update UI to reflect that editing has finished and output is being rendered.

    // Render and provide output on a background queue.
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
        // Create editing output from the editing input.
        PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input];
        NSURL *originalImageURL = self.input.fullSizeImageURL;
        //Apply filter
        CIFilter *appliedFilter = [CIFilter filterWithLUT:self.presetFilterNameArray[self.currentAppliedFilterIndex] originalLUT:@"LUT-ORG-512" dimension:64 alpha:self.filterAlphaSlider.value CIContext:self.ciContext];
        UIImage *filteredOriginalImage = [self filterApply:[UIImage imageWithContentsOfFile:originalImageURL.path] filter:appliedFilter];
        //Apply orientation
        filteredOriginalImage = [UIImage imageWithCGImage:filteredOriginalImage.CGImage scale:filteredOriginalImage.scale orientation:self.input.fullSizeImageOrientation];

        // Provide new adjustments and render output to given location.
        NSData *archiver = [NSKeyedArchiver archivedDataWithRootObject:self.presetFilterNameArray[self.currentAppliedFilterIndex]];
        output.adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"com.fantagram.BetterAndBetter.TKPhotoExtension" formatVersion:@"1.0" data:archiver];

        NSData *renderedJPEGData = UIImageJPEGRepresentation(filteredOriginalImage, 1.0f);
        if([renderedJPEGData writeToURL:output.renderedContentURL atomically:YES]){
            NSLog(@"success to write");
        }
        else{
            NSLog(@"fail to write");
        }

        NSLog(@"%@", output);
        // Call completion handler to commit edit to Photos.
        completionHandler(output);

        // Clean up temporary files, etc.
    });
}

Previous Answer for this issue, someone said that when rendering to NSData, the size of image need to be different with originalImage. So I tried this, but didn't work.

One More Thing.

Are there any way to open the host app from photo extension?

self.extensionContext openURL: didn't work(As I know of It only works in Today Extension.)

UIResponder *responder = self;
        while(responder){
            if([responder respondsToSelector:@selector(openURL:)]){
                dispatch_async(dispatch_get_main_queue(), ^{
                       [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:(MY_URL_Schemes)]];     
                });
            }
            responder = [responder nextResponder];
        }

above code also didn't work. It worked when I used it share extension.

Thanks.

2
  • Regarding your second question: It's in the documentation, but the iOS SDK's APIs that can be used from within an extension are limited. In particular, the whole UIApplication business is out of bounds (it makes sense: your extension is simply a shared framework being used by another app, which owns the sharedInstance of UIApplication). Thus, I don't think you can launch the container app from within the extension. Sep 16, 2018 at 2:17
  • I created a minimal app based on Apple's sample code, except that it attempts to save the original image unchanged; the rest of finishContentEditing: is the same as Apple's code. I've logged all the properties of the PHContentEditingInput data passed when the editing session begins, and I soon noticed that saving always fails exactly when fullSizeImageOrientation has the value 6 (a.k.a CGImagePropertyOrientation.right), which somehow applies to all phtots I take in portrait orientation. As soon as I figure out a workaround or solution, I will post an. answer. Sep 17, 2018 at 5:37

1 Answer 1

-1

Try this,

Go to SETTINGS then PHOTOS & CAMERA. I changed the options from Optimize iPhone Storage, to DOWNLOAD & KEEP ORIGINALS.

I think Normally under the Optimize setting photos are downloaded back to the original and then you are allowed to edit.

Hope it will help you.

3
  • 1
    Thanks, but the other published photo extension I downloaded from App store works well. I think it not a problem of iPhone Setting
    – TKang
    Dec 27, 2016 at 8:45
  • I’m having the same issue. Some apps from the store experience it too, some don’t. Apple’s sample code also suffers from it. It seems to occur on specific photos. Sep 15, 2018 at 5:18
  • @TKang I downloaded several extensions, as well as Apple's own sample code. Surprisingly, Apple's code and some of the third-party extensions (but not all) on the Store experience the issue as well, with some photos. A notably robust one, which seems to always be able to save successfully, is BitCam. Sep 16, 2018 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.