如何从消息扩展创建 GIF 贴纸
How to create GIF stickers from Messages extension
我构建了一个 消息应用程序扩展程序,灵感来自 Ice Cream Builder(Apple 示例代码):https://developer.apple.com/library/content/samplecode/IceCreamBuilder/Introduction/Intro.html
我想使用动画 gif,而不是使用 "png" 图片作为我的贴纸。因为gif做贴纸更好玩!
在示例代码中,贴纸是从 xcassets 文件夹中的文件 URL 创建的,PNG 格式。
func sticker(for mySticker: MySticker, completion: @escaping (_ sticker: MSSticker) -> Void) {
// Determine the URL for the sticker.
let fileName = mySticker.name! + ".png"
let url = cacheURL.appendingPathComponent(fileName)
// Create an operation to process the request.
let operation = BlockOperation {
// Check if the sticker already exists at the URL.
let fileManager = FileManager.default
guard !fileManager.fileExists(atPath: url.absoluteString) else { return }
// Create the sticker image and write it to disk.
guard let image = UIImage(named:mySticker.name!), let imageData = UIImagePNGRepresentation(image) else { fatalError("Unable to build image for stickers") }
do {
try imageData.write(to: url, options: [.atomicWrite])
} catch {
fatalError("Failed to write sticker image to cache: \(error)")
}
}
// Set the operation's completion block to call the request's completion handler.
operation.completionBlock = {
do {
let sticker = try MSSticker(contentsOfFileURL: url, localizedDescription: "My Sticker")
completion(sticker)
} catch {
print("Failed to write image to cache, error: \(error)")
}
}
// Add the operation to the queue to start the work.
queue.addOperation(operation)
}
我试图编辑文件名:
let fileName = mySticker.name! + ".gif"
...不过当然不行
我注意到仅当我在 xcassets 文件夹中创建一个 "Sticker pack" 时,动画 GIF 格式才适用于 Xcode。
但我的代码无法使用它,此行出错:
guard let image = UIImage(named:mySticker.name!), let imageData = UIImagePNGRepresentation(image) else { fatalError("Unable to build image for stickers") }
这似乎合乎逻辑,但我找不到解决方法。
解决方案非常简单。直接使用路径.gif。
请注意,如果您的 gif 在特定文件夹中,则此文件夹不能是文件夹引用。
func sticker(for mySticker: MySticker, completion: @escaping (_ sticker: MSSticker) -> Void) {
let fileName = mySticker.name + ".gif"
let url = cacheURL.appendingPathComponent(fileName)
// Create an operation to process the request.
let operation = BlockOperation {
// Check if the sticker already exists at the URL.
let fileManager = FileManager.default
guard !fileManager.fileExists(atPath: url.absoluteString) else { return }
// Create the sticker image and write it to disk.
guard let image = UIImage(named:mySticker.name), let imageData = UIImagePNGRepresentation(image) else { fatalError("Unable to build image \(mySticker.name)") }
do {
try imageData.write(to: url, options: [.atomicWrite])
} catch {
fatalError("Failed to write sticker image \(mySticker.name) to cache: \(error)")
}
}
// Set the operation's completion block to call the request's completion handler.
operation.completionBlock = {
do {
let sticker = try MSSticker(contentsOfFileURL: url, localizedDescription: "My Sticker")
completion(sticker)
} catch {
print("Failed to write image to cache, error: \(error)")
}
}
}
和
struct MySticker {
var name: String
var num: Int
}
我构建了一个 消息应用程序扩展程序,灵感来自 Ice Cream Builder(Apple 示例代码):https://developer.apple.com/library/content/samplecode/IceCreamBuilder/Introduction/Intro.html
我想使用动画 gif,而不是使用 "png" 图片作为我的贴纸。因为gif做贴纸更好玩!
在示例代码中,贴纸是从 xcassets 文件夹中的文件 URL 创建的,PNG 格式。
func sticker(for mySticker: MySticker, completion: @escaping (_ sticker: MSSticker) -> Void) {
// Determine the URL for the sticker.
let fileName = mySticker.name! + ".png"
let url = cacheURL.appendingPathComponent(fileName)
// Create an operation to process the request.
let operation = BlockOperation {
// Check if the sticker already exists at the URL.
let fileManager = FileManager.default
guard !fileManager.fileExists(atPath: url.absoluteString) else { return }
// Create the sticker image and write it to disk.
guard let image = UIImage(named:mySticker.name!), let imageData = UIImagePNGRepresentation(image) else { fatalError("Unable to build image for stickers") }
do {
try imageData.write(to: url, options: [.atomicWrite])
} catch {
fatalError("Failed to write sticker image to cache: \(error)")
}
}
// Set the operation's completion block to call the request's completion handler.
operation.completionBlock = {
do {
let sticker = try MSSticker(contentsOfFileURL: url, localizedDescription: "My Sticker")
completion(sticker)
} catch {
print("Failed to write image to cache, error: \(error)")
}
}
// Add the operation to the queue to start the work.
queue.addOperation(operation)
}
我试图编辑文件名:
let fileName = mySticker.name! + ".gif"
...不过当然不行
我注意到仅当我在 xcassets 文件夹中创建一个 "Sticker pack" 时,动画 GIF 格式才适用于 Xcode。
但我的代码无法使用它,此行出错:
guard let image = UIImage(named:mySticker.name!), let imageData = UIImagePNGRepresentation(image) else { fatalError("Unable to build image for stickers") }
这似乎合乎逻辑,但我找不到解决方法。
解决方案非常简单。直接使用路径.gif。 请注意,如果您的 gif 在特定文件夹中,则此文件夹不能是文件夹引用。
func sticker(for mySticker: MySticker, completion: @escaping (_ sticker: MSSticker) -> Void) {
let fileName = mySticker.name + ".gif"
let url = cacheURL.appendingPathComponent(fileName)
// Create an operation to process the request.
let operation = BlockOperation {
// Check if the sticker already exists at the URL.
let fileManager = FileManager.default
guard !fileManager.fileExists(atPath: url.absoluteString) else { return }
// Create the sticker image and write it to disk.
guard let image = UIImage(named:mySticker.name), let imageData = UIImagePNGRepresentation(image) else { fatalError("Unable to build image \(mySticker.name)") }
do {
try imageData.write(to: url, options: [.atomicWrite])
} catch {
fatalError("Failed to write sticker image \(mySticker.name) to cache: \(error)")
}
}
// Set the operation's completion block to call the request's completion handler.
operation.completionBlock = {
do {
let sticker = try MSSticker(contentsOfFileURL: url, localizedDescription: "My Sticker")
completion(sticker)
} catch {
print("Failed to write image to cache, error: \(error)")
}
}
}
和
struct MySticker {
var name: String
var num: Int
}