Skip to main content
Language:

From Camera Roll

PhotoEditor SDK supports importing photos from the camera roll.

Instantiate image picker#

To that end, we first instantiate a UIImagePickerController. We are opting for this class over PHPickerViewController, because the latter does not reliably work for some .mp4 files encoded with non-ffmpeg codecs. We have reported this issue to Apple.

We further set the current class as the controller's delegate to handle cancellation. Since we only want photos from the camera roll, we set sourceType and mediaType correspondingly.

Initialize the editor#

The imagePickerController delegate is invoked when the user picked a photo and the photo object is passed to it as originalImage via the info dictionary. We can now dismiss the image picker and create a Photo from the image which is, in turn, passed as photoAsset to the PhotoEditViewController to instantiate the photo editor. Again, setting the current class as the delegate of the PhotoEditViewController allows us to implement export, cancellation and error handling there.

File:
import AVFoundation
import PhotoEditorSDK
import UIKit
import UniformTypeIdentifiers
class OpenPhotoFromCameraRollSwift: Example, UIImagePickerControllerDelegate, UINavigationControllerDelegate, PhotoEditViewControllerDelegate {
override func invokeExample() {
// We recommend using the old `UIImagePickerController` class instead of the newer `PHPickerViewController`
// due to video import issues for some MP4 files encoded with non-ffmpeg codecs. We've filed a bug report
// with Apple regarding this issue. If you do not require support for video files, feel free to use
// `PHPickerViewController`.
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
imagePicker.mediaTypes = [UTType.image.identifier]
// Present the image picker modally.
imagePicker.modalPresentationStyle = .fullScreen
presentingViewController?.present(imagePicker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let image = info[.originalImage] as? UIImage else {
fatalError()
}
// Dismiss the image picker and present the photo editor after dismissal is done.
presentingViewController?.dismiss(animated: true, completion: {
// Create a `Photo` from the picked photo.
let photo = Photo(image: image)
// 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 = .fullScreen
self.presentingViewController?.present(photoEditViewController, animated: true, completion: nil)
})
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
presentingViewController?.dismiss(animated: true, completion: nil)
}
// MARK: - PhotoEditViewControllerDelegate
func 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)
}
}