Kingfisher is a sophisticated image loading and caching library for Apple platforms, designed with a modular architecture that promotes separation of concerns and extensibility. At its core, the library employs a coordinator pattern where KingfisherManager serves as the central orchestrator, managing the flow between network operations, caching layers, and image processing pipelines. The architecture leverages protocol-oriented design principles, with all functionality exposed through a .kf namespace wrapper that provides a clean, chainable API surface.
The system is built on three fundamental pillars: downloading, caching, and processing. The ImageDownloader handles all network operations with support for authentication, retries, and progressive loading. The ImageCache implements a dual-layer caching strategy combining memory and disk storage for optimal performance. The ImageProcessor protocol enables a flexible transformation pipeline where multiple processors can be chained together. These components work in concert through a sophisticated options system (KingfisherOptionsInfo) that allows fine-grained control over every aspect of the image loading process.
Cross-platform compatibility is achieved through extensive use of conditional compilation and type aliases, allowing the same codebase to support iOS, macOS, tvOS, watchOS, and visionOS. The library provides both UIKit/AppKit extensions and dedicated SwiftUI components (KFImage, KFAnimatedImage), ensuring seamless integration regardless of the UI framework being used.
| Component | Location | Purpose |
|---|---|---|
| KingfisherManager | Sources/General/KingfisherManager.swift |
Central coordinator managing image retrieval, caching, and processing workflows |
| ImageDownloader | Sources/Networking/ImageDownloader.swift |
Handles all network operations for downloading images |
| ImageCache | Sources/Cache/ImageCache.swift |
Dual-layer caching system with memory and disk storage |
| ImageProcessor | Sources/Image/ImageProcessor.swift |
Protocol and implementations for image transformation pipeline |
| KF | Sources/General/KF.swift |
Builder pattern entry point for fluent API |
| Source | Sources/General/ImageSource/Source.swift |
Represents image data sources (network/provider) |
| Resource | Sources/General/ImageSource/Resource.swift |
Protocol for cacheable resources with key/URL |
| Component | Location | Purpose |
|---|---|---|
| MemoryStorage | Sources/Cache/MemoryStorage.swift |
In-memory cache implementation with LRU eviction |
| DiskStorage | Sources/Cache/DiskStorage.swift |
File-based cache with expiration and size limits |
| CacheSerializer | Sources/Cache/CacheSerializer.swift |
Handles image data serialization for cache storage |
| Component | Location | Purpose |
|---|---|---|
| SessionDelegate | Sources/Networking/SessionDelegate.swift |
URLSession delegate for download management |
| SessionDataTask | Sources/Networking/SessionDataTask.swift |
Wrapper for URLSessionDataTask with cancellation |
| ImagePrefetcher | Sources/Networking/ImagePrefetcher.swift |
Preloads images for improved performance |
| RequestModifier | Sources/Networking/RequestModifier.swift |
Protocol for modifying URL requests |
| RetryStrategy | Sources/Networking/RetryStrategy.swift |
Configurable retry logic for failed downloads |
| Component | Location | Purpose |
|---|---|---|
| ImageView+Kingfisher | Sources/Extensions/ImageView+Kingfisher.swift |
UIImageView/NSImageView extensions |
| KFImage | Sources/SwiftUI/KFImage.swift |
SwiftUI image component |
| KFAnimatedImage | Sources/SwiftUI/KFAnimatedImage.swift |
SwiftUI animated image support |
| AnimatedImageView | Sources/Views/AnimatedImageView.swift |
GIF animation support view |
The heart of the library, containing:
shared singleton instance (line 113)retrieveImage() main entry point (lines 196-210, 233-248)Network layer implementation:
ImageLoadingResult struct for download results (lines 36-58)DownloadTask class for cancellable downloads (lines 65-102)Caching infrastructure:
CacheType enum defining cache levels (lines 52-72)Builder pattern implementation:
Processing pipeline:
ImageProcessItem enum for input types (lines 37-46)ImageProcessor protocol definition (lines 49-76)append() (lines 85-95)Configuration system:
UIImageView.kf.setImage(with: url)
│
└─> ImageView+Kingfisher.swift (line 77-87)
│
└─> KingfisherManager.retrieveImage()
Sources/General/KingfisherManager.swift (line 196)
KingfisherManager.retrieveImage()
│
└─> retrieveImageFromCache() (line 400)
│
├─> ImageCache.retrieveImage()
│ Sources/Cache/ImageCache.swift
│ │
│ ├─> MemoryStorage.value(forKey:)
│ │ Sources/Cache/MemoryStorage.swift
│ │
│ └─> DiskStorage.value(forKey:)
│ Sources/Cache/DiskStorage.swift
│
└─> [Cache Hit] → completionHandler(.success)
[Cache Miss] → Continue to download
KingfisherManager.loadAndCacheImage() (line 415)
│
└─> ImageDownloader.downloadImage()
Sources/Networking/ImageDownloader.swift
│
├─> SessionDelegate.downloadTask()
│ Sources/Networking/SessionDelegate.swift
│
└─> URLSession.dataTask()
│
└─> CompletionHandler with ImageLoadingResult
Downloaded Data
│
└─> ImageProcessor.process() (line 434)
Sources/Image/ImageProcessor.swift
│
├─> DefaultImageProcessor (if none specified)
│
└─> Custom processors chain
│
└─> Processed KFCrossPlatformImage
Processed Image
│
└─> KingfisherManager.cacheImage() (line 459)
│
├─> ImageCache.store() (line 482)
│ │
│ ├─> MemoryStorage.store()
│ │ In-memory cache with cost calculation
│ │
│ └─> DiskStorage.store()
│ File system with expiration
│
└─> completionHandler(.success(RetrieveImageResult))
│
└─> UI Update on main queue
Download/Processing Error
│
└─> RetryStrategy.retry() (line 362)
Sources/Networking/RetryStrategy.swift
│
├─> [Retry] → startNewRetrieveTask() (line 306)
│
└─> [No Retry] → Check alternative sources (line 334)
│
├─> [Alternative exists] → Start new task
│
└─> [No alternatives] → completionHandler(.failure)
This architecture enables Kingfisher to efficiently handle image loading with features like progressive downloading, multiple cache layers, flexible processing pipelines, and robust error handling, all while maintaining a clean and intuitive API surface for developers.