From Remote URL
VideoEditor SDK supports loading stickers from a remote URL, this can be a resource hosted by a hosting provider or your own servers. The sticker tool is optimized for remote resources which allows to directly integrate a remote URL instead of downloading the asset beforehand. For an example of how to download the remote resources in advance see the font example.
In order to load stickers from a remote URL, we pass the 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.
import UIKitimport VideoEditorSDKclass VideoAddStickersFromRemoteURLSwift: Example, VideoEditViewControllerDelegate {override func invokeExample() {// Create a `Video` from a URL to a video in the app bundle.let video = Video(url: Bundle.main.url(forResource: "Skater", withExtension: "mp4")!)// Create a custom sticker.// The sticker tool is optimized for remote resources which allows to directly// integrate a remote URL instead of downloading the asset before. For an// example on how to download the remote resources in advance and use the local// downloaded resources, see: Examples/Text/AddFonts/FromRemoteURL.let customSticker = Sticker(imageURL: URL(string: "https://img.ly/static/example-assets/custom_sticker_igor.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: URL(string: "https://img.ly/static/example-assets/custom_sticker_igor.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 video editor. Make this class the delegate of it to handle export and cancelation.let videoEditViewController = VideoEditViewController(videoAsset: video, configuration: configuration)videoEditViewController.delegate = selfvideoEditViewController.modalPresentationStyle = .fullScreenpresentingViewController?.present(videoEditViewController, animated: true, completion: nil)}// MARK: - VideoEditViewControllerDelegatefunc videoEditViewControllerShouldStart(_ videoEditViewController: VideoEditViewController, task: VideoEditorTask) -> Bool {// Implementing this method is optional. You can perform additional validation and interrupt the process by returning `false`.true}func videoEditViewControllerDidFinish(_ videoEditViewController: VideoEditViewController, result: VideoEditorResult) {// The user exported a new video successfully and the newly generated video is located at `result.output.url`. Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}func videoEditViewControllerDidFail(_ videoEditViewController: VideoEditViewController, error: VideoEditorError) {// There was an error generating the video.print(error.localizedDescription)// Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}func videoEditViewControllerDidCancel(_ videoEditViewController: VideoEditViewController) {// The user tapped on the cancel button within the editor. Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}}