Kingfisher — what it is, key concepts, and ImageCache

Author: IT Sectr Published: 2026-05-05 Reading time: 8 min

Kingfisher is a library for loading and caching images on iOS, macOS, and watchOS, written in pure Swift. According to the official repository, the library provides full support for Swift Concurrency, Combine, and SwiftUI, as well as automatic two-level caching. Kingfisher is known for its type-safe API and easy integration with Swift projects.

Key Takeaways

  • Kingfisher is an image loading library in pure Swift with support for async/await, Combine, and SwiftUI.
  • KingfisherManager is a single entry point for loading, tracking cache and network requests.
  • ImageCache implements two-level storage: Memory Cache and Disk Cache with configurable limits.
  • ImageProcessor is a protocol for transformations: resizing, rounding, blurring, watermarking.
  • KFImage is a View component for SwiftUI with declarative description of loading states.

What is Kingfisher?

Kingfisher is a library for asynchronous image loading and caching on Apple platforms, written entirely in Swift. The library author is Wei Wang (onevcat). Kingfisher provides a set of tools for loading images from the network with automatic caching, transformations, and support for modern Swift technologies: async/await, Combine, Sendable.

The library has over 23,000 stars on GitHub and is used in applications such as Telegram, Snapchat, and Dropbox. Kingfisher supports GIF, APNG, HEIF, and all standard image formats. Each request returns a type-safe Result, eliminating type casting errors.

The architecture of Kingfisher is built on three main components: Manager (download manager), Cache (two-level cache), and Processor (transformations). These components are connected through protocols, allowing any part to be replaced without changing dependencies.

How does Kingfisher work: Manager and Cache

KingfisherManager is the central class that coordinates image loading, caching, and processing. It holds references to ImageCache and ImageDownloader and provides a single method retrieveImage that returns a ready-to-use image after going through all stages.

Loading process

When calling retrieveImage, the Manager first checks Memory Cache — an NSCache with UIImage, where the key is formed from the source URL and CacheSerializer. If the image is found, it is returned immediately. On a miss, it checks Disk Cache — reading from the file system with decryption via serializer. If the disk cache is empty, a network request is made through ImageDownloader, the result is decoded, transformed, and saved to both cache levels.

  • Memory Cache — NSCache-based, automatically cleared on memory warning
  • Disk Cache — file storage with TTL and size limit checking
  • ImageDownloader — URLSession-based with request modification support via modifiers

Swift Concurrency

Starting from version 7.0, Kingfisher fully supports async/await. The retrieveImage method is available as an asynchronous function, returning Result directly without completion handlers. This allows using the library in modern Swift architectures with Structured Concurrency.

Core modules of Kingfisher

Kingfisher is divided into several modules, each solving its own task. This separation simplifies testing and component replacement.

KingfisherManager

KingfisherManager is a facade that combines loading, cache, and processors. By default, the singleton KingfisherManager.shared is used, but a separate instance with custom settings can be created for isolated scenarios (e.g., for unit tests).

ImageCache

ImageCache is a two-level cache with separate settings for memory and disk. Memory Cache has no limit on the number of objects but is cleared by the system when memory is low. Disk Cache stores files in a directory with configurable TTL (default 7 days), size limit (default 0 — no limit), and automatic cleanup.

ImageProcessor

ImageProcessor is a protocol with a single method process(item:options:) that returns the processed image. Built-in implementations: ResizingImageProcessor (resizing), RoundCornerImageProcessor (rounding), BlurImageProcessor (Gaussian blur), OverlayImageProcessor (color overlay). Processors can be combined using the |> operator.

Kingfisher caching system

The Kingfisher cache architecture is based on the write-through principle: data is written to both levels simultaneously, and reading starts from the fastest level — memory. The cache key is the absolute image URL after removing query parameters.

ParameterMemory CacheDisk Cache
StorageNSCache (RAM)File system (SSD)
FormatUIImage (decoded)Data (compressed, via serializer)
CleanupUIApplication.didReceiveMemoryWarningNotificationTTL + exceeding limit
SerializationNot requiredCacheSerializer (default PNG/JPEG)
Thread safetyYes (synchronized access)Yes (IO queue + barriers)

To manage Disk Cache size, the total file size is calculated with sorting by last access date. When the limit is exceeded, files with the oldest access date are removed until the size falls below 50% of the limit. TTL cleanup occurs during cache initialization and on each call to cleanExpired.

Examples of using Kingfisher in Swift

Kingfisher provides several interfaces for loading images: an extension on UIImageView, a separate manager, and a SwiftUI View.

Loading in UIImageView via kf

kf is a namespace property on UIImageView, providing setImage, cancelDownload methods, and loading indicators. The setImage method accepts URLSource and optional parameters Options and completionHandler.

swift
import Kingfisher

imageView.kf.setImage(
    with: URL(string: "https://example.com/image.jpg"),
    placeholder: UIImage(named: "placeholder"),
    options: [
        .processor(RoundCornerImageProcessor(radius: .point(12))),
        .transition(.fade(0.3)),
        .cacheMemoryOnly
    ],
    progressBlock: { receivedSize, totalSize in
        print("Loaded \(receivedSize) / \(totalSize)")
    }
)

The method returns a DownloadTask that supports cancellation via cancel and progress tracking. Internally, setImage calls KingfisherManager.shared.retrieveImage with automatic ImageView detection as Target.

Using with async/await

Starting from Kingfisher 7.0, the setImage method is available in an async version. This allows integrating image loading into Swift Structured Concurrency without callbacks.

swift
func loadAvatar() async {
    do {
        let result = try await imageView.kf.setImage(
            with: url,
            options: [.processor(ResizingImageProcessor(
                targetSize: CGSize(width: 100, height: 100)
            ))]
        )
        // result.image contains UIImage
    } catch {
        print("Failed: \(error)")
    }
}

KFImage for SwiftUI

KFImage is a SwiftUI View, similar to AsyncImage from iOS 15, but with full Kingfisher caching support. The View automatically uses KingfisherManager.shared but supports a custom manager through the .configure modifier.

swift
struct AvatarView: View {
    let url: URL

    var body: some View {
        KFImage(url)
            .placeholder { ProgressView() }
            .resizable()
            .fade(duration: 0.25)
            .forceTransition()
            .frame(width: 80, height: 80)
            .cornerRadius(40)
    }
}

Loading indicators in Kingfisher

Kingfisher provides a built-in indicator system for displaying image loading progress. IndicatorType is an enum with three options: .activity (UIActivityIndicatorView), .progress (UIProgressView), and .custom (custom implementation of the Indicator protocol). The indicator automatically appears above the ImageView during loading and is hidden after completion.

For a custom indicator, you need to implement the Indicator protocol with startAnimatingView() and stopAnimatingView() methods. This allows for hybrid solutions: a skeleton with shimmer animation, a placeholder image with gradual reveal, or a logo with opacity animation. Kingfisher also supports setting the indicator globally via KingfisherManager.shared.defaultOptions.

Kingfisher vs SDWebImage

On the iOS platform, Kingfisher and SDWebImage are the two dominant image loading libraries. The choice between them depends on the project language, performance requirements, and ecosystem.

CriteriaKingfisherSDWebImage
LanguageSwift (100%)Objective-C + Swift
Async/AwaitNative supportVia wrapper
CombineBuilt-in PublisherNo
SendableSupportsLimited
Type safetyFull (Result type)Via Any?
ImageProcessorComposite via |> Transformer via &&
Size~900 KB~1.2 MB
GitHub Stars23,000+25,000+

The key advantage of Kingfisher is its Swift-first architecture: full support for async/await, Combine Publishers, Sendable, and Result types. SDWebImage maintains leadership due to its broader plugin ecosystem (WebP, SVG, MapKit) and Objective-C support.

Setting up Kingfisher in an iOS project

Kingfisher is installed via Swift Package Manager, CocoaPods, or Carthage. After installation, simply import the module and call any loading method — the library is ready to use without additional configuration.

swift
// Swift Package Manager (Package.swift)
dependencies: [
    .package(
        url: "https://github.com/onevcat/Kingfisher.git",
        from: "7.12.0"
    )
]

// CocoaPods (Podfile)
pod 'Kingfisher', '~> 7.12'

For customizing global settings, use KingfisherManager.shared. You can change the downloader timeout, cache strategy, and default processors. Below is an example of configuring a 500 MB cache with a 14-day TTL.

swift
let cache = ImageCache(name: "custom")
cache.memoryStorage.config.totalCostLimit = 100 * 1024 * 1024
cache.diskStorage.config.sizeLimit = 500 * 1024 * 1024
cache.diskStorage.config.expiration = .days(14)
KingfisherManager.shared.cache = cache

ImageDownloader is also configured through the manager: you can set a custom URLSessionConfiguration with timeouts, headers, and caching policies. For progress monitoring, the KFIndicator module is available with support for ActivityIndicator, ProgressView, and custom indicators.

Frequently Asked Questions

What is Kingfisher and what is it used for?

Kingfisher is a library for loading images on iOS, written in pure Swift. It is used for asynchronous loading, caching, and transforming images from the network with full integration into SwiftUI, UIKit, and modern Swift technologies.

How to install Kingfisher via Swift Package Manager?

Add the package https://github.com/onevcat/Kingfisher.git with version from 7.12.0 in Xcode via File → Add Packages. Or specify the dependency in Package.swift with the parameter from: "7.12.0". After installation, import the Kingfisher module.

What image formats does Kingfisher support?

Kingfisher supports JPEG, PNG, GIF, APNG, HEIF, and WebP. All formats are decoded through system frameworks (ImageIO, CoreGraphics). GIF is supported via CGImageSource with progressive loading and animation.

What is the advantage of Kingfisher over SDWebImage?

Kingfisher is written in pure Swift and fully supports async/await, Combine, and Sendable. It provides a type-safe Result API and a modular architecture through protocols, simplifying component replacement and testing.

How to clear Kingfisher cache?

To clear Memory Cache, call KingfisherManager.shared.cache.clearMemoryCache(). For Disk Cache, use clearDiskCache(). To remove only expired files — cleanExpiredDiskCache(). Cache size can be checked via cache.calculateDiskStorageSize().

Summary

  • Kingfisher is a modern image loading library in pure Swift with support for all relevant Apple technologies.
  • Two-level cache (Memory + Disk) with configurable limits and TTL ensures fast access and minimal traffic usage.
  • Async/Await and Combine allow embedding loading into any architecture without callbacks and delegates.
  • KFImage for SwiftUI provides a declarative API with placeholder, error, and custom transition effects.
  • ImageProcessor with composition via the |> operator provides flexibility in creating transformation chains.
  • Type safety Result eliminates runtime errors when processing results.
  • Modular architecture through protocols allows replacing Manager, Cache, and Downloader for testing and customization.

We will develop a mobile application turnkey

IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.

Discuss the project

Read also