Skip to main content
PESDK/iOS/Guides/Brush
Language:

Configure Brush

PhotoEditor SDK supports several configuration options for the BrushToolController allowing flexible adaptation to different needs and use cases. For a detailed explanation of how to configure different editor views, refer to this guide.

allowedBrushTools#

By default, all available brush tools are enabled. For this example only a couple are enabled.

defaultBrushColor#

The default color of the brush stroke is UIColor.white. If you can anticipate that your use case requires a different default color you can set the defaultBrushColor.

defaultBrushSize#

Similarly, the size of the brush is set at 5% relative to the smaller side of the photo by default. In our example, we prefer to set it to an absolute size of five pixels.

availableColors#

In order to customize the available color palette, we need to configure the BrushColorToolController setting the option availableColors to an array of Color objects. Here, we only provide a small selection of colors as might be sensible for an annotation use case.

File:
import PhotoEditorSDK
import UIKit
class PhotoBrushConfigurationSwift: 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 the `BrushToolController` which lets the user
// draw on the image.
builder.configureBrushToolController { options in
// By default all available brush tools are enabled.
// For this example only a couple are enabled.
options.allowedBrushTools = [.color, .size]
// By default the default color for the brush stroke is
// `UIColor.white`. For this example the default color
// is set to `UIColor.black`.
options.defaultBrushColor = UIColor.black
// By default the default brush size is set to 5% of the
// smaller side of the image.
// For this example the default size should be absolute.
options.defaultBrushSize = FloatValue.absolute(5.0)
}
// Configure the `BrushColorToolController` which lets the user
// change the color of the brush stroke.
builder.configureBrushColorToolController { options in
// By default the editor provides a variety of different
// colors to customize the color of the brush stroke.
// For this example only a small selection of colors is enabled.
options.availableColors = [
Color(color: UIColor.white, colorName: "White"),
Color(color: UIColor.black, colorName: "Black"),
Color(color: UIColor.red, colorName: "Red")
]
}
}
// 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 = self
photoEditViewController.modalPresentationStyle = .fullScreen
presentingViewController?.present(photoEditViewController, 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)
}
}