Download APNG File

647 views Asked by At

I am getting some issues related to APNG file, APNG file animation working perfect if i put APNG files in resource bundle , But when i have download same APNG file from assets server and saving APNG file into resource directory and then load using MSSticker like this way. after loading it showing only first frame.if anyone wanna try to check APNG file please have a look to this.

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

Here i am saving image & getting saved image url from resource directory:

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
}

I have written the extension in custom 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)
}
1

There are 1 answers

0
Bilal On BEST ANSWER

I think problem is this UIImagePNGRepresentation. Why convert Data to UIImage and then use UIImagePNGRepresentation.

Try saving data directly.

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
    }
}

And ignore image just pass data.

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