下载 APNG 文件

Download APNG File

我遇到了一些与 APNG 文件相关的问题,如果我将 APNG 文件放在资源包中,APNG 文件动画效果很好,但是当我从资产服务器下载相同的 APNG 文件并将 APNG 文件保存到资源目录中,然后加载像这样使用 MSSticker。加载后只显示第一个 frame.if 任何想尝试检查 APNG 文件的人请看看这个。

let imagePath = Bundle.main.path(forResource: imgName, ofType: ".png")

    let pathurl =  URL(fileURLWithPath: imagePath!)

    do {
        try cell.stickerview.sticker = MSSticker(contentsOfFileURL: pathurl, localizedDescription: "anything that you want")

    }
    catch {
        fatalError("Failed to create sticker: \(error)")
    }

我在这里保存图片并从资源目录中获取保存的图片url:

static func saveImage(image: UIImage , name:String) -> Bool? {

    guard let data = UIImagePNGRepresentation(image) else {
        return false
    }
    guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
        return false
    }
    do {
        try data.write(to: directory.appendingPathComponent(name)!)
        return true
    } catch {
        print(error.localizedDescription)
        return false
    }
}

static func getSavedImageUrl(named: String) -> URL? {
    if let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
        return URL(fileURLWithPath: dir.absoluteString).appendingPathComponent(named)
    }
    return nil
}

我已经在自定义中编写了扩展 MSSticker class

extension MSStickerView {
func downloadedFrom(url: URL , name: String) {
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
            let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
            let data = data, error == nil,
            let image = UIImage(data: data)
            else { return }
        DispatchQueue.main.async() { () -> Void in
            // self.sticker = image
            _ = GameUtil.saveImage(image: image, name: name)

            if let pathurl = GameUtil.getSavedImageUrl(named: name) {
                do {

                     try self.sticker = MSSticker(contentsOfFileURL: pathurl, localizedDescription: "Raid")
                }
                catch {
                    fatalError("Failed to create sticker: \(error)")
                }
            }
            self.startAnimating()
        }
        }.resume()
}
func downloadedFrom(link: String , name: String) {
    guard let url = URL(string: link) else { return }
    downloadedFrom(url: url ,name: name)
}

我认为问题是这个UIImagePNGRepresentation。为什么要将 Data 转换为 UIImage 然后使用 UIImagePNGRepresentation.

尝试直接保存数据。

static func saveData(data: Data , name:String) -> Bool? {

    guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
        return false
    }
    do {
        try data.write(to: directory.appendingPathComponent(name)!)
        return true
    } catch {
        print(error.localizedDescription)
        return false
    }
}

并忽略图像只是传递数据。

_ = GameUtil.saveImage(data: data, name: name)