Advanced Swift framework for loading, processing, caching, displaying and preheating images.
var request = ImageRequest(URLRequest: <#NSURLRequest#>)
request.targetSize = CGSize(width: 200, height: 200) // Resize image
request.processor = <#ImageProcessing#> // Apply image filters
Nuke.taskWithRequest(request) { response in
let image = response.image
}.resume()
- Zero config & user-friendly
- Performant, asynchronous, thread safe
- Optional Alamofire and AnimatedImage plugins
- Nuke is a pipeline that loads images using injectable dependencies
- Beautiful playground, complete documentation and wiki included
- Uses NSURLSession with HTTP/2 support
- Uses a single data task for multiple equivalent requests
- Automated preheating of images close to the viewport
- Full featured extensions for UI components
- Doesn't reinvent caching, relies on HTTP cache and its implementation in Foundation
- Caching is transparent to the client
- Two cache layers including auto purging memory cache
- Create, compose and apply image filters
- Background image decompression and scaling in a single step
- Resize loaded images to fit displayed size
- iOS 8.0+ / watchOS 2.0+ / OS X 10.9+ / tvOS 9.0+
- Xcode 7.1+, Swift 2.0+
- Get a demo project using
pod try Nuke
command - Experiment with Nuke in a playground
- Install,
import Nuke
and enjoy!
Nuke.taskWithURL(imageURL) {
let image = $0.image
}.resume()
var request = ImageRequest(URLRequest: <#NSURLRequest#>)
request.targetSize = CGSize(width: 300.0, height: 400.0) // Set target size in pixels
request.contentMode = .AspectFill
Nuke.taskWithRequest(request) {
let image = $0.image // Image is resized
}.resume()
Nuke.taskWithRequest(request) { response in
switch response {
case let .Success(image, info):
// Use image and inspect info
case let .Failure(error):
// Handle error
}
}.resume()
let task = Nuke.taskWithURL(imageURL).resume()
task.progress = { completed, total in
// Update progress
}
let state = task.state // Track task state
task.completion { // Add multiple completions, even for completed task
let image = $0.image
}
task.cancel()
let imageView = UIImageView()
// let task = imageView.nk_setImageWithURL(<#NSURL#>)
let task = imageView.nk_setImageWithRequest(<#ImageRequest#>, options: <#ImageViewLoadingOptions?#>)
Nuke makes it extremely easy to add full-featured image loading extensions to UI components
extension MKAnnotationView: ImageDisplayingView, ImageLoadingView {
// That's it, you get default implementation of all methods in ImageLoadingView protocol
public var nk_image: UIImage? {
get { return self.image }
set { self.image = newValue }
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellReuseID, forIndexPath: indexPath)
let imageView: ImageView = <#view#>
imageView.nk_prepareForReuse()
imageView.nk_setImageWithURL(imageURL)
return cell
}
Cancel image task as soon as the cell goes offscreen (optional):
func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
let imageView: ImageView = <#view#>
imageView.nk_prepareForReuse()
}
let filter1: ImageProcessing = <#filter#>
let filter2: ImageProcessing = <#filter#>
let filterComposition = ImageProcessorComposition(processors: [filter1, filter2])
var request = ImageRequest(URL: <#image_url#>)
request.processor = filterComposition
Nuke.taskWithRequest(request) {
// Filters are applied, filtered image is stored in memory cache
let image = $0.image
}.resume()
let processor1: ImageProcessing = <#processor#>
let processor2: ImageProcessing = <#processor#>
let composition = ImageProcessorComposition(processors: [processor1, processor2])
let requests = [ImageRequest(URL: imageURL1), ImageRequest(URL: imageURL2)]
Nuke.startPreheatingImages(requests: requests)
Nuke.stopPreheatingImages(requests: requests)
let preheater = ImagePreheatingControllerForCollectionView(collectionView: <#collectionView#>)
preheater.delegate = self // Signals when preheat window changes
let dataLoader: ImageDataLoading = <#dataLoader#>
let decoder: ImageDecoding = <#decoder#>
let cache: ImageMemoryCaching = <#cache#>
let configuration = ImageManagerConfiguration(dataLoader: dataLoader, decoder: decoder, cache: cache)
ImageManager.shared = ImageManager(configuration: configuration)
Protocol | Description |
---|---|
ImageManager |
A top-level API for managing images |
ImageDataLoading |
Performs loading of image data (NSData ) |
ImageDecoding |
Converts NSData to UIImage objects |
ImageProcessing |
Processes decoded images |
ImageMemoryCaching |
Stores processed images into memory cache |
To install Nuke add a dependency to your Podfile:
# source 'https://github.com/CocoaPods/Specs.git'
# use_frameworks!
# platform :ios, "8.0" / :watchos, "2.0" / :osx, "10.9" / :tvos, "9.0"
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 "Nuke-AnimatedImage-Plugin" # optional
Import installed modules in your source files
import Nuke
import NukeAlamofirePlugin
import NukeAnimatedImagePlugin
- Nuke Alamofire Plugin - Alamofire plugin for Nuke that allows you to use Alamofire for networking
- Nuke AnimatedImage Plugin - FLAnimatedImage plugin for Nuke that allows you to load and display animated GIFs
- Nuke Integration Tests - Contains CocoaPods and Carthage integration tests for Nuke
![](https://cloud.githubusercontent.com/assets/1567433/6521218/9c7e2502-c378-11e4-9431-c7255cf39577.png)
![](https://cloud.githubusercontent.com/assets/1567433/6521243/fb085da4-c378-11e4-973e-1eeeac4b5ba5.png)
![](https://cloud.githubusercontent.com/assets/1567433/6521256/20247bc2-c379-11e4-8e9e-417123debb8c.png)
Nuke is available under the MIT license. See the LICENSE file for more info.