Skip to main content
PESDK/iOS/Guides/Stickers/Add Stickers
Language:

From App Bundle

PhotoEditor SDK supports loading stickers from the app bundle. In this example, we load both a custom sticker and an existing sticker from the img.ly bundle.

In order to load stickers from our bundle, we pass the bundle URL for our resource to the Sticker constructor along with an identifier. Sticker objects, additionally, hold metadata such as a thumbnailURL and an identifier. The sticker identifier needs to be unique since it is used during the (de)serialization process.

We then need to add the stickers to an StickerCategory object which holds the metadata of the category such as a preview image and a title.

Finally we can configure the assetCatalog by appending the custom sticker to the array of sticker categories.

File:
import PhotoEditorSDK
import UIKit
class PhotoAddStickersFromAppBundleSwift: 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 custom sticker.
let customSticker = Sticker(imageURL: Bundle.main.url(forResource: "custom_sticker_igor", withExtension: "png")!, thumbnailURL: nil, identifier: "custom_sticker_igor")
// Use an existing sticker from the img.ly bundle.
let existingSticker = Sticker(imageURL: Bundle.imgly.resourceBundle.url(forResource: "imgly_sticker_emoticons_laugh", withExtension: "png")!, thumbnailURL: nil, identifier: "existing_sticker")
// Assign the stickers to a new custom sticker category.
let customStickerCategory = StickerCategory(identifier: "custom_sticker_category", title: "Custom", imageURL: Bundle.main.url(forResource: "custom_sticker_igor", withExtension: "png")!, stickers: [customSticker, existingSticker])
// Create a `Configuration` object.
let configuration = Configuration { builder in
// In this example we are using the default assets for the asset catalog as a base.
// However, you can also create an empty catalog as well which only contains your
// custom assets.
let assetCatalog = AssetCatalog.defaultItems
// Add the category to the asset catalog.
assetCatalog.stickers.append(customStickerCategory)
// Use the new asset catalog for the configuration.
builder.assetCatalog = assetCatalog
}
// 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)
}
}