Failed to Save in Photos Extension

I've start editing an image in Photos on macOS with my extension, and then when I try to save, I get an alert saying something went wrong when saving.

Code Block
func finishContentEditing(completionHandler:)


In the finish content editing method I save my photo async like this:

Code Block
let output = PHContentEditingOutput(contentEditingInput: input)
let unitCrop: UnitCrop = UnitCrop(frame: self.cropper.frame)
do {
let data: Data = try JSONEncoder().encode(unitCrop)
output.adjustmentData = PHAdjustmentData(formatIdentifier: UnitCrop.formatIdentifier,
formatVersion: UnitCrop.formatVersion,
data: data)
} catch {
completionHandler(nil)
return
}
self.cropper.save { result in
switch result {
case .success(let image):
guard let imageData: Data = image.pngData() else {
completionHandler(nil)
return
}
do {
try imageData.write(to: output.renderedContentURL, options: .atomic)
print("success!")
completionHandler(output)
} catch {
completionHandler(nil)
return
}
case .failure(let error):
completionHandler(nil)
}
}


I see the success print, tho I still get an popup alert saying it failed in Photos and no edits gets saved.

Do anyone know how to debug this?

Accepted Answer
Does you extension process log contain something along the lines of "Failed to save content editing output %@ with error %@"?
You could look at logs for both Photos.app and your extension in Console.app.

From the code above it looks like an issue with the written out image. PHContentEditingOutput expects JPEG for the rendered images.

From the documentation of renderedContentURL:

Read this property to find a URL for writing edited asset content. Then, if editing a photo asset, write the altered photo image to a file in JPEG format at this URL. If editing a video asset, export the video to a QuickTime (.mov) file at this URL.


Hi, thanks for the tip about the console logs, I found a log saying "Error: Image is not JPEG or HEIF". I did a test and the jpg format works!
It's too bad I can't save a lossless png. My app is for editing screenshots. Tho at least I can save something now! Thanks.
Failed to Save in Photos Extension
 
 
Q