Chris Hannah

Loading Images From the Web Into a UIImageView

This is more of a recommendation than a tip, but it totally helped me out, so I thought I’d share.

If you find yourself needing to load quite a substantial about of images form the web, handle caching, and then add them to image views, then I think Kingfisher is a library you should check out.

I use it in my client, Slate, where I’m loading profile pictures, and countless images inside the post content. I simply pass the URL of the image to the UIImageView, and Kingfisher will take care of the downloading, caching, applying, and also apply a placeholder while it’s doing this if you so choose.

Straight from the README on GitHub, the easiest implementation of this is as follows:

let url = URL(string: "url_of_your_image")
imageView.kf.setImage(with: url)

But there is a more complex function you can call to have a whole load more control:

func setImage(with resource: Resource?,
                         placeholder: Placeholder? = nil,
                         options: KingfisherOptionsInfo? = nil,
                         progressBlock: DownloadProgressBlock? = nil,
                         completionHandler: CompletionHandler? = nil) -> RetrieveImageTask

Which you can use like this:

let imageURL = URL(string: "here is the image url")
        let placeholderImage = UIImage(named: "placeholder")
    imageView.kf.setImage(with: imageURL,
                          placeholder: placeholderImage,
                          options: nil, progressBlock: { (receivedSize, totalSize) in
                            // Do something with the progress amount, like update a progress bar
    }) { (image, error, cacheType, url) in
        // After completion, do whatever you want with the image, any errors, cacheType, and the URL

I’m a big fan of the library, and if you’re ever in a situation where you need to simply load a few images from the web, or a more image heavy app, then this is my suggestion. It’s certainly a lot easier than writing your own image downloader, and cache system. I spent a while making one myself, but it was never as good as Kingfisher.

Kingfisher on GitHub

#Kingfisher #Library #UIImageView

Reply via Email