Kingfisher for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Kingfisher: What It Is and Why You Need It



Kingfisher is a powerful and popular library for Swift, designed for downloading and caching images from the network. It provides developers with a simple yet flexible API for working with images in iOS, macOS, tvOS, and watchOS applications. The library is written in pure Swift and is actively maintained by the community.



Why do you need Kingfisher? In modern mobile applications, working with images is one of the key tasks. Users expect fast content loading, smooth scrolling, and minimal data consumption. Kingfisher solves these problems by automatically managing the downloading, caching (in memory and on disk), and displaying of images. Instead of writing complex code to manage network requests, error handling, and caching, you can simply call one method setImage(with:).



Key Features:

  • Asynchronous loading: images are loaded in the background thread without blocking the UI.
  • Multi-level caching: first checks the memory cache (fast access), then the disk cache (persistent storage).
  • Error handling: built-in mechanisms for handling network errors, invalid URLs, and corrupted data.
  • GIF and SVG support: ability to display animated images and vector graphics.
  • Image transformations: built-in methods for resizing, cropping, applying filters, and rounding corners.
  • Integration with UIKit and AppKit: extensions for UIImageView, NSImageView, UIButton, and other elements.
  • Download cancellation: automatic cancellation of requests when reusing cells in UITableView/UICollectionView.


Kingfisher is not just an image loader, it is a full-fledged framework for managing media content that saves hours of development and improves application performance.



Installing Kingfisher



Kingfisher can be installed in several ways. The recommended method is via Swift Package Manager (SPM), as it is a native tool for managing dependencies in Swift.



1. Swift Package Manager (SPM) — Recommended Method

Open your project in Xcode, go to File → Add Packages... and paste the repository URL:

https://github.com/onevcat/Kingfisher.git

Select the version (recommended "Up to Next Major Version" with the latest stable version) and add the package to your target.



2. CocoaPods

Add to your Podfile:

pod 'Kingfisher', '~> 7.0'

Then run in the terminal:

pod install


3. Carthage

Add to your Cartfile:

github "onevcat/Kingfisher" ~> 7.0

Then run:

carthage update


4. Manual Installation

Download the latest release from GitHub and add the files from the Sources/Kingfisher folder to your project.



After installation, don't forget to import the library in the files where you plan to use it:

import Kingfisher


Quick Start: First Example



Let's start with the simplest example — loading an image from a URL and displaying it in a UIImageView. This is the basic operation that begins your work with Kingfisher.



import UIKitimport Kingfisher

class ViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() let url = URL(string: "https://example.com/image.jpg")! imageView.kf.setImage(with: url) }}


In this example, we call the setIm

Recommendations