onevcat / APNGKit

High performance and delightful way to play with APNG format in iOS.
MIT License
2.21k stars 200 forks source link

Can I load APNG image from URL ? #94

Closed TungVuDuc2805 closed 2 years ago

uhooi commented 4 years ago

@TungVuDuc2805 You can load APNG Image from URL by adding an initializer to APNGImage with Extension.

import APNGKit

extension APNGImage {
    convenience init?(urlString: String?) {
        guard let urlString = urlString, let url = URL(string: urlString) else {
            return nil
        }
        self.init(url: url)
    }

    convenience init?(url: URL) {
        guard let data = try? Data(contentsOf: url) else {
            return nil
        }
        self.init(data: data)
    }
}

Usage:

apngImageView.image = APNGImage(urlString: "https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png")
apngImageView.startAnimating()

ref: https://github.com/uhooi/APNGSample/blob/master/APNGSample/APNGImage%2BURL.swift

onevcat commented 2 years ago

As indicated by @uhooi this is done for now. There is a plan to also support built-in downloading and cache with the help of another library Kingfisher as a plugin. But that is another story. I am closing this for now.