Skip to main content

Present Photo Editor using SwiftUI

Since we are using UIKit in our example application we need to use an UIHostingController to present a SwiftUI View. The PhotoEditorSwiftUIView defined below wraps the actual PhotoEditor view to coordinate its dismissal with the presenting UIViewController. This process is not necessary for a pure SwiftUI application. You would use the PhotoEditor view directly.

Create the photo editor#

The custom PhotoEditorSwiftUIView acts as the ContentView of a pure SwiftUI application. The PhotoEditor is declared to be the only view and to be initialized with the provided photo. The dismissAction is responsible to forward the events from the PhotoEditor modifiers to the hosting UIViewController.

Handling events#

Using the onDidSave modifier allows us to register an event handler that is invoked when a user successfully exports an edited photo. The result argument passed to the closure is of type PhotoEditorResult and contains the data of the exported photo.

After performing some action with this photo, such as uploading it or saving it, we call dismissAction to exit the editor.

Likewise, if the user taps the cancel button or an error was thrown we dismiss the editor in this example.

Next Steps#