To a Remote URL
PhotoEditor SDK supports saving photos to a remote server.
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.
Create a Request Object#
In order to upload the exported photo, create a URLRequest
object for the web service that is supposed to receive the photo upload and set the httpMethod
to POST
.
Create an Upload Task#
Using the URLSession
networking API we create an uploadTask
passing the data
, urlRequest
and a closure to handle the request outcome.
In this example, we simply log the status code and potential errors. In a production app you would display some message to the user communicating the status of the request.
Start Upload Task in Background#
Invoking the method resume
on the upload tasks starts it in the background, allowing us to dismiss the view controller immediately.