Language:
From Local Path
PhotoEditor SDK supports loading photo files from the local file system such as the documents or a temporary folder, as well as the app bundle.
Retrieve bundle URL#
First we need to retrieve the URL for a file on the local system. In this example, a file within the app bundle.
Initialize the editor#
We then create a Photo
from this URL and pass it as photoAsset
to the PhotoEditViewController
to instantiate the photo editor.
Setting the current class as the delegate of the PhotoEditViewController
allows us to implement export, cancellation and error handling there.
File:
import PhotoEditorSDKimport UIKitclass OpenPhotoFromAppBundleSwift: Example, PhotoEditViewControllerDelegate {override func invokeExample() {// Get the URL for a file on the local file system. This could be any file within the app bundle// or also a file within the documents or temporary folders for example. This example will use// a file from the app bundle.guard let url = Bundle.main.url(forResource: "LA", withExtension: "jpg") else {fatalError("Unable to create URL for specified file.")}// Create a `Photo` from the photo URL.let photo = Photo(url: url)// Create a photo editor and pass it the photo. Make this class the delegate of it to handle export and cancelation.let photoEditViewController = PhotoEditViewController(photoAsset: photo)photoEditViewController.delegate = self// Present the photo editor.photoEditViewController.modalPresentationStyle = .fullScreenpresentingViewController?.present(photoEditViewController, animated: true, completion: nil)}// MARK: - PhotoEditViewControllerDelegatefunc photoEditViewControllerShouldStart(_ photoEditViewController: PhotoEditViewController, task: PhotoEditorTask) -> Bool {// Implementing this method is optional. You can perform additional validation and interrupt the process by returning `false`.true}func photoEditViewControllerDidFinish(_ photoEditViewController: PhotoEditViewController, result: PhotoEditorResult) {// The image has been exported successfully and is passed as an `Data` object in the `result.output.data`.// To create an `UIImage` from the output, use `UIImage(data:)`.// See other examples about how to save the resulting image.presentingViewController?.dismiss(animated: true, completion: nil)}func photoEditViewControllerDidFail(_ photoEditViewController: PhotoEditViewController, error: PhotoEditorError) {// There was an error generating the photo.print(error.localizedDescription)// Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}func photoEditViewControllerDidCancel(_ photoEditViewController: PhotoEditViewController) {// The user tapped on the cancel button within the editor. Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}}