A lightweight, fast, and highly configurable Swift image loading library with built-in caching and cancellation. Works seamlessly with UIKit, AppKit, and SwiftUI β making it suitable for iOS, macOS, tvOS, and watchOS applications.
- π§ Smart in-memory & disk caching
- π§Ή Automatic cancellation for reused views
- ποΈ Configurable via
SwiftlyImageLoaderConfiguration
- β Retry logic and TTL configuration
- π§© Modular targets: UIKit / AppKit / SwiftUI
- π Zero dependencies, pure Swift
- π Great for performance-sensitive use cases (e.g. fast-scrolling lists)
When you request an image:
Request Image
β
Check in-memory cache (fast, volatile)
β
If not found β Check disk cache (persistent)
β
If not found β Download from network
β
Save to memory + disk caches for future use
- Memory Cache (
ImageCache
) β uses NSCache, evicts on memory pressure - Disk Cache (
DiskCache
) β saves across app launches, TTL-aware
This ensures blazing-fast UI (via RAM) + reduced network usage (via disk).
ImageLoader.setup(with: SwiftlyImageLoaderConfiguration(
memoryCacheTTL: 60, // In-memory cache expires after 60 seconds
diskCacheTTL: 86400, // Disk cache expires after 24 hours
autoCancelOnReuse: true, // Cancel previous task for reused views
enableBatchCancelation: true, // Allows cancelAll() to stop all loading tasks
logLevel: .verbose // Enables verbose logging for debugging
))
Add this line to your Package.swift
:
.package(url: "https://github.com/mohsinbmwm3/SwiftlyImageLoader.git", from: "1.0.0")
Then add one or more of the following modules to your target:
.product(name: "SwiftlyImageLoader", package: "SwiftlyImageLoader"),
.product(name: "SwiftlyImageLoaderUIKit", package: "SwiftlyImageLoader"),
.product(name: "SwiftlyImageLoaderAppKit", package: "SwiftlyImageLoader"),
.product(name: "SwiftlyImageLoaderSwiftUI", package: "SwiftlyImageLoader")
Customize behavior via the SwiftlyImageLoaderConfiguration
:
ImageLoader.setup(with: SwiftlyImageLoaderConfiguration(
autoCancelOnReuse: true,
enableBatchCancelation: true,
logLevel: .verbose
))
Property | Description |
---|---|
autoCancelOnReuse |
Cancels prior image loads for the same URL automatically |
enableBatchCancelation |
Allows cancelling all tasks via ImageLoader.cancelAll() |
logLevel |
Controls log verbosity (none , basic , verbose ) |
import SwiftlyImageLoaderUIKit
imageView.setImage(from: URL(string: "https://picsum.photos/id/45/800/600"))
Supports:
- Placeholder images
- Image reuse cancellation via config
- Memory & disk cache fallback
import SwiftlyImageLoaderAppKit
imageView.setImage(from: URL(string: "https://picsum.photos/id/55/1200/800"))
import SwiftlyImageLoaderSwiftUI
SwiftlyAsyncImage(url: URL(string: "https://picsum.photos/id/33/600/400"))
Sources/
βββ SwiftlyImageLoader // Core engine, caching, config
βββ SwiftlyImageLoaderUIKit // UIImageView extensions
βββ SwiftlyImageLoaderAppKit // NSImageView extensions
βββ SwiftlyImageLoaderSwiftUI // SwiftlyAsyncImage wrapper
Use Instruments to track:
- Memory spikes
- Network reuse / caching hit rates
- Logging behavior with
.verbose
mode
Use .cancelAll()
in viewDidDisappear()
for clean teardown.
MIT Β© Mohsin Khan