我得到了一些与APNG文件相关的问题,如果我把APNG文件放在资源包中,APNG文件动画工作得很好,但当我从资产服务器下载相同的APNG文件并将APNG文件保存到资源目录中,然后像这样使用MSSticker加载时。加载后只显示第一帧。如果有人想尝试检查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)")
    }

ios - 下载APNG文件-LMLPHP
在这里,我正在保存图像&从资源目录获取保存的图像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类中编写了扩展
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
尝试直接保存数据。

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)

10-07 15:46