基于Ice Cream Builder的Messages扩展:如何将PNG贴纸替换为GIF?
Hey there! Let’s sort out why your GIF stickers aren’t working after swapping out the PNGs from the Ice Cream Builder sample. The core issue here is that Messages app extensions don’t play nicely with GIFs stored in asset catalogs, plus the sticker initialization code needs a small tweak for animated content. Here’s how to get your dynamic stickers up and running:
1. Move Your GIFs Out of xcassets
Asset catalogs (xcassets) are designed for static images—they don’t handle animated GIFs properly for Messages stickers. Instead:
- Create a new folder in your Messages extension target (name it something like
AnimatedStickersfor clarity) - Drag your GIF files into this folder, making sure to check "Copy items if needed" and select your extension target in the "Add to targets" dropdown.
2. Update the Sticker Initialization Code
The original sample uses UIImage(named:) to pull PNGs from the asset catalog, but for GIFs, you need to load the file’s direct URL. Head to the code where MSSticker objects are created (usually in a class like StickerBrowserViewController):
Replace this static PNG-focused code:
guard let image = UIImage(named: stickerFileName), let imageURL = Bundle.main.url(forResource: stickerFileName, withExtension: "png") else { return nil } let sticker = try? MSSticker(contentsOfFileURL: imageURL, localizedDescription: "")
With this GIF-friendly version:
guard let gifURL = Bundle.main.url(forResource: stickerFileName, withExtension: "gif") else { return nil } let sticker = try? MSSticker(contentsOfFileURL: gifURL, localizedDescription: "")
Pro tip: Double-check that stickerFileName matches your GIF’s filename (without the .gif extension) to avoid URL loading failures.
3. Ensure Your GIFs Meet Messages Requirements
Messages has strict rules for animated stickers to work smoothly:
- Keep file size under 500KB (larger GIFs might fail to animate or load entirely)
- Stick to a frame rate between 15-30 FPS (higher rates waste bandwidth without visible benefits)
- Use dimensions no larger than 300x300px (stickers are displayed small, so extra resolution is unnecessary)
4. Double-Check Target Inclusion
If your GIFs still don’t show up, verify they’re added to your extension’s build phase:
- In Xcode, select your Messages extension target
- Go to Build Phases > Copy Bundle Resources
- Make sure your GIF files are listed here—if not, click the
+button to add them.
After making these changes, build and run your app, then open Messages to test your extension. Your animated GIF stickers should now play when sent or viewed!
内容的提问来源于stack exchange,提问作者cmii

