Export an UIImage as a sticker for Messages app

641 views Asked by At

I'm trying to export an UIImage as a sticker for Messages app, but I can't find any documentation about this.

I have my project with the Sticker Pack extension enabled and see that Xcode created a new Stickers.xcassets, every image that I add there manually appears on Messages app to use as a sticker, but how can I save an image programatically there, so the user can create stickers from any image ?

All I can find is information related to creating stickers without doing any code, just shipping an app with predefined images.

let sticker = try MSSticker(contentsOfFileURL: url, localizedDescription: "Test")

I know how create a MSSticker from the image file, but after this I don't know how to proceed and make this newly created sticker appear on Messages app.

1

There are 1 answers

2
Denis Kakačka On

Stickers are loaded from stickers assets catalog .xcstickers. This catalog is a resource file/folder, that means this is placed inside your bundle. All files inside your bundle are not writable and they have only read permissions. IMO what you need to do is:

  • You have to store your users custom images in any one of the sandboxed folders (Documents, Library and temp).

  • Read image from this folder and create MSSticker from it same way as you did. - let sticker = try MSSticker(contentsOfFileURL: url, localizedDescription: "Test") and add. this sticker to array.

  • In your MSStickerBrowserViewController you will load stickers from array created in previous step.

Like this

override func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
    stickers.count
}

override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView,
                                 stickerAt index: Int) -> MSSticker {
    stickers[index]
}