Loading images asynchronously in Swift iOS 02.04.2020

There are two two popular libraries that do the trick in just a few lines of code: SDWebImage or Kingfisher. Both are easy to get via CocoaPods and are quite similar in API and supported features.

SDWebImage

Add the SDWebImage pod to the project:

pod 'SDWebImage'

Given an image view and a URL to load the image from, loading the image with an activity indicator is as simple as:

import SDWebImage
...
if let imageURL = URL(string: "URL_HERE") {
    imageView.sd_setShowActivityIndicatorView(true)
    imageView.sd_setIndicatorStyle(.gray)
    imageView.sd_setImage(with: imageURL)
}

Kingfisher

Add the Kingfisher pod to the project:

pod 'Kingfisher'

Loading an image from an URL with activity indicator and displaying it in the image view:

import Kingfisher
...
if let imageURL = URL(string: "URL_HERE") {
    imageView.kf.indicatorType = .activity
    imageView.kf.setImage(with: imageURL)
}

Familiarize yourself with Kingfisher via cheat sheet.