Micro-framework for loading, processing, caching and preheating images.
- Simple API, zero configuration required
- Performant, asynchronous, thread-safe
- Hassle-free image loading into image views (and other targets)
- Two cache layers including auto-purging LRU memory cache
- Image transformations
- Automated preheating (prefetching)
- Pipeline with injectable dependencies
- Alamofire and FLAnimatedImage plugins
- Homepage
- Documentation
- Demo project (
pod try Nuke
)
Nuke allows for hassle-free image loading into image views (and other arbitrary targets).
Nuke.loadImage(with: URL(string: "http://...")!, into: imageView)
Each image request is represented by Request
struct which can be created with either URL
or URLRequest
.
You can add an arbitrary number of image processors to the request.
Nuke.loadImage(with: Request(url: url).process(with: GaussianBlur()), 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 inherits `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(string: "http://...")!, token: cts.token)
.then { image in print("\(image) loaded") }
.catch { error in print("catched \(error)") }
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 |
Loads data |
DataCaching |
Stores data into disk cache |
DataDecoding |
Converts data into image objects |
Processing |
Image transformations |
Caching |
Stores images into memory cache |
- iOS 9.0 / watchOS 2.0 / macOS 10.11 / tvOS 9.0
- Xcode 8, Swift 3
To install Nuke add a dependency to your Podfile:
# source 'https://github.com/CocoaPods/Specs.git'
# use_frameworks!
pod "Nuke"
pod "Nuke-Alamofire-Plugin" # optional
pod "Nuke-AnimatedImage-Plugin" # optional
To install Nuke add a dependency to your Cartfile:
github "kean/Nuke"
github "kean/Nuke-Alamofire-Plugin" # optional
github "kean/Nuke-AnimatedImage-Plugin" # optional
Nuke is available under the MIT license. See the LICENSE file for more info.