代码之家  ›  专栏  ›  技术社区  ›  cmii

如何从消息扩展创建GIF标签

  •  1
  • cmii  · 技术社区  · 8 年前

    我建立了一个 消息应用程序扩展 ,灵感来源于冰淇淋生成器(苹果示例代码): 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") }
    

    这似乎合乎逻辑,但我找不到解决办法。

    1 回复  |  直到 8 年前
        1
  •  0
  •   cmii    8 年前

    解决方案非常简单。直接使用路径。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
    }