To Camera Roll
PhotoEditor SDK supports saving photos to the camera roll.
For the sake of this example, we create a photo from a URL and present the photo editor in a modal.
Note, that we have to make the current class the delegate of the PhotoEditViewController
in order to handle export and cancellation there.
Implementing the Photo Export Delegate Method#
When a photo export is successful the method photoEditViewControllerDidFinish
is invoked on the delegate class and a PhotoEditorResult
object passed as argument.
The result.output.data
attribute will contain the output image data, including all EXIF information in the format specified in your editor's configuration.
If no modifications have been made to the photo and the Photo
object that was passed to the editor's initializer was created using Photo(data:)
or Photo(url:)
we will not process the photo at all and simply forward it to this delegate method.
If the Photo
object that was passed to the editor's initializer was created using Photo(image:)
, it will be processed and returned in the format specified in your editor's configuration.
If you want to ensure that the original photo is always reencoded, even if no modifications have
been made to it, you can set PhotoEditViewControllerOptions.forceExport
to true
, in which case result.output.data
will
always point to a newly generated photo.
Request Authorization#
In order to access a user's camera roll we have to request access using the PHPhotoLibrary
.
We check the status that is passed to the closure to ascertain whether authorization has been granted and handle the case when it is denied.
In a production app you would display an alert explicitly requesting that the user edit their settings to grant access.
Add to Shared Library#
Once access has been granted, we can perform changes in the shared photo library. To that end we create a PHAssetCreationRequest
and invoke the addResource
method passing the photo data.
Finally, we handle the completion of the change operation. Accessing the status arguments success
and error
allows us to log errors and display messages to our users before we dispatch to main queue and dismiss the editor.