A powerful image loading and caching framework which allows for hassle-free image loading in your app - often in one line of code.
Nuke pulls together stable, mature libraries from Swift ecosystem into simple, lightweight package that lets you focus on getting things done.
- Simple and expressive API, zero configuration required
- Hassle-free image loading into image views and other targets
- Two cache layers including LRU memory cache
- Extensible image transformations
- Freedom to use networking, caching libraries of your choice
- Plugins: Alamofire, FLAnimatedImage, Toucan
- Automated prefetching with Preheat library
- Performant, supports large (or infinite) collection views of images
- Comprehensive test coverage
Upgrading from the previous version? Use a migration guide.
Nuke allows for hassle-free image loading into image views and other targets.
Nuke.loadImage(with: url, into: imageView)
Nuke.loadImage(with:into:)
method cancels previous outstanding request associated with the target. No need to implement prepareForReuse
. The requests also get cancelled automatically when the target deallocates (Nuke holds a weak reference to a target).
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
Nuke.loadImage(with: url, into: cell.imageView)
}
You can also (optionally) implement collectionView(didEndDisplaying:forItemAt:)
method to cancel the request as soon as the cell goes off screen:
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
Nuke.cancelRequest(for: cell.imageView)
}
Each image request is represented by Request
struct. It can be created with either URL
or URLRequest
and then further customized.
// Create and customize URLRequest
var urlRequest = URLRequest(url: url)
urlRequest.cachePolicy = .reloadIgnoringLocalCacheData
urlRequest.timeoutInterval = 30
var request = Request(urlRequest: urlRequest)
// You can add arbitrary number of transformations to the request
request.process(with: GaussianBlur())
// Disable memory caching
request.memoryCacheOptions.writeAllowed = false
// Load an image
Nuke.loadImage(with: request, into: imageView)
You can specify custom image processors using Processing
protocol which consists of a single method process(image: Image) -> Image?
. Here's an example of custom image filter that uses Core Image:
struct GaussianBlur: Processing {
var radius = 8
func process(image: UIImage) -> UIImage? {
return image.applyFilter(CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius" : self.radius]))
}
// `Processing` protocol requires `Equatable` to identify cached images
func ==(lhs: GaussianBlur, rhs: GaussianBlur) -> Bool {
return lhs.radius == rhs.radius
}
}
Preheating (prefetching) means loading images ahead of time in anticipation of its use. Nuke provides a Preheater
class that does just that:
let preheater = Preheater()
// User enters the screen:
let requests = [Request(url: url1), Request(url: url2), ...]
preheater.startPreheating(for: requests)
// User leaves the screen:
preheater.stopPreheating(for: requests)
You can use Nuke in combination with Preheat library which automates preheating of content in UICollectionView
and UITableView
.
let preheater = Preheater()
let controller = Preheat.Controller(view: collectionView)
controller.handler = { addedIndexPaths, removedIndexPaths in
preheater.startPreheating(for: requests(for: addedIndexPaths))
preheater.stopPreheating(for: requests(for: removedIndexPaths))
}
One of the Nuke's core classes is Loader
. Its API and implementation is based on Promises. You can use it to load images directly.
let cts = CancellationTokenSource()
Loader.shared.loadImage(with: url, token: cts.token)
.then { image in print("\(image) loaded") }
.catch { error in print("catched \(error)") }
Allows you to replace networking layer with Alamofire. Combine the power of both frameworks!
FLAnimatedImage plugin allows you to load and display animated GIFs with smooth scrolling performance and low memory footprint.
Toucan plugin provides a simple API for processing images. It supports resizing, cropping, rounded rect masking and more.
Nuke is designed to support and leverage dependency injection. It consists of a set of protocols - each with a single responsibility - that come together in an object graph that manages loading, decoding, processing, and caching images. You can easily create and use/inject your own implementations of the following core protocols:
Protocol | Description |
---|---|
Loading |
Loads images |
DataLoading |
Downloads data |
DataCaching |
Stores data into disk cache |
DataDecoding |
Converts data into image objects |
Processing |
Image transformations |
Caching |
Stores images into memory cache |
You can learn more from an in-depth Nuke 4 Migration Guide.
- iOS 9.0 / watchOS 2.0 / macOS 10.11 / tvOS 9.0
- Xcode 8
- Swift 3
Nuke is available under the MIT license. See the LICENSE file for more info.