Updated 30 August 2024
Greetings readers, today in our learning journey we will learn how to use Kingfisher to download images in Swift.
When developing an iOS application, managing images efficiently is crucial for both performance and user experience. Managing network images on the iOS Application can be a bit tricky, especially In a scenario where you are getting images from the internet.
To tackle such situations, we usually need to download the images and then need to display them through the URL Session. In This blog, we will use an alternative and easy solution to tackle such a situation through Kingfisher.
Kingfisher is a powerful and pure Swift-implemented library for downloading and caching images from the web. The Downloaded images through the Kingfisher will be cached in both memory and disk. So there is no need to download it again which can significantly boost your app performance.
The most powerful feature of Kingfisher is everything in Kingfisher goes asynchronously from downloading to caching the network images. That means you can never worry about blocking your UI main thread.
You can use the Kingfisher for your network images either by installing the Podfile (Cocopods) or through SPM (Swift Package Management). For this blog, we will use SPM to install the dependency on our project.
Once the package is added to your project, import the kingfisher into your file and add a UIImageView on the respective UIViewController.
1 2 3 4 5 6 7 8 9 10 |
import UIKit import Kingfisher class ViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } } |
Now, use the URL of the image from the network and create the image URL through the URLSession.
1 |
let imageUrl = URL(string: "https://forthriverstrust.org/wp-content/uploads/2019/01/kingfisher.jpg") |
Once you completed the above steps, all that remains is to use the kingfisher to add the imageUrl into our UIImageView.
1 |
imageView.kf.setImage(with: imageUrl) |
The Kingfisher provides the feature to handle the compilation and gives a chance to handle the error case. You can simply add the handle inside the setImage method along with your imageurl.
1 2 3 4 5 6 7 8 9 10 |
imageView.kf.setImage(with: imageUrl, completionHandler: { result in switch result { case .success(let value): print("Image: \(value.image), URL: \(value.source.url?.absoluteString ?? "")") break case .failure(let error): print("Error: \(error.localizedDescription)") break } }) |
Once you complete all the mentioned steps, you should be able to see the below output.
In Conclusion, we learned that the Kingfisher is a powerful and flexible tool for managing images in your Swift applications and how to implement it on your application.
You can check out more amazing blogs with us through Mobikul Blogs.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.