0

我遇到了一些与 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)")
    }

在此处输入图像描述

在这里,我正在保存图像并从资源目录中获取保存的图像 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)
}
4

1 回答 1

1

我认为问题是这样的UIImagePNGRepresentation。为什么转换DataUIImage然后使用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)
于 2017-12-08T03:15:55.543 回答