gpt4 book ai didi

ios - 如何从消息扩展创建 GIF 贴纸

转载 作者:行者123 更新时间:2023-11-28 07:53:59 29 4
gpt4 key购买 nike

我构建了一个消息应用程序扩展,灵感来自 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 文件夹中创建“贴纸包”时,动画 GIF 格式才能在 Xcode 中使用。

enter image description here

但是我的代码不能用它,这一行的错误:

 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
}

关于ios - 如何从消息扩展创建 GIF 贴纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48835440/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com