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.
What is 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.
How to use Kingfisher in Swift
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.
Step 1:- Add Kingfisher Dependency
- Open the project in Xcode and go to the file->Add Package Dependency
- Search for the Kingfisher package in the search bar and click on the Add Package.
Step 2:- Use the Kingfisher Dependency
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) |
Step 3:- Use the Completion Handler
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 } }) |
Output:-
Once you complete all the mentioned steps, you should be able to see the below output.
Conclusion
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.