gpt4 book ai didi

objective-c - 下载 APNG 文件

转载 作者:行者123 更新时间:2023-11-28 15:10:41 24 4
gpt4 key购买 nike

我遇到了一些与 APNG 文件相关的问题,如果我将 APNG 文件放在资源包中,APNG 文件动画效果很好,但是当我从 Assets 服务器下载相同的 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)")
}

enter image description here

我在这里保存图片并从资源目录获取保存的图片 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 然后使用 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)

关于objective-c - 下载 APNG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47698712/

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