Language:
Customize Menu Items
PhotoEditor SDK supports full control over the menu items used in the editor UI.
Configure menu items#
The tool menu items displayed in the main menu can be configured through the PhotoEditViewController
options.
The property menuItems
takes an array of PhotoEditMenuItems
.
Here, we first create an array of MenuItems
which will be converted to a PhotoEditMenuItem
.
Note that items for tools not included in your license subscription will be hidden automatically.
PhotoEditMenuItem
#
PhotoEditMenuItem
#PhotoEditMenuItem
is an enum with two possible cases:
case tool(ToolMenuItem)
represents a tool that can be pushed onto the stack. It has aToolMenuItem
as an associated value, which has a title, an icon and the class of the tool that should be instantiated.case action(ActionMenuItem)
represents an action that should be executed when this menu item is selected. It has anActionMenuItem
as an associated value, which has a title, an icon, the closure that should be executed and which can update the current photo edit model, and a state closure that is used to query the active state of the action.
The .tool
case can be used to present any subclass of PhotoEditToolController
, making it easier to add your very own custom tool controllers to the SDK.
File:
import PhotoEditorSDKimport UIKitclass PhotoCustomizeMenuItemsSwift: Example, PhotoEditViewControllerDelegate {override func invokeExample() {// Create a `Photo` from a URL to an image in the app bundle.let photo = Photo(url: Bundle.main.url(forResource: "LA", withExtension: "jpg")!)// Create a `Configuration` object.let configuration = Configuration { builder in// Configure settings related to the `PhotoEditViewController`.builder.configurePhotoEditViewController { options in// Configure the available tool menu items to be displayed in the main menu.// Menu items for tools not included in your license subscription will be hidden automatically.options.menuItems = [// Create the default `MenuItem`s.ToolMenuItem.createTransformToolItem(),ToolMenuItem.createFilterToolItem(),ToolMenuItem.createAdjustToolItem(),ToolMenuItem.createFocusToolItem(),ToolMenuItem.createStickerToolItem(),ToolMenuItem.createTextToolItem(),ToolMenuItem.createTextDesignToolItem(),ToolMenuItem.createOverlayToolItem(),ToolMenuItem.createFrameToolItem(),ToolMenuItem.createBrushToolItem(),ActionMenuItem.createMagicItem()].compactMap { menuItem in// Convert `MenuItem`s to `PhotoEditMenuItem`s.switch menuItem {case let menuItem as ToolMenuItem:return .tool(menuItem)case let menuItem as ActionMenuItem:return .action(menuItem)default:return nil}}.sorted { a, b in// Sort the menu items by their title for demonstration purposes.func title(_ menuItem: PhotoEditMenuItem) -> String {switch menuItem {case .tool(let menuItem):return menuItem.titlecase .action(let menuItem):return menuItem.titledefault:return ""}}return title(a) < title(b)}}}// Create and present the photo editor. Make this class the delegate of it to handle export and cancelation.let photoEditViewController = PhotoEditViewController(photoAsset: photo, configuration: configuration)photoEditViewController.delegate = selfphotoEditViewController.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)}}