Skip to content
/ Nuke Public
forked from kean/Nuke

Image loading, processing, caching and preheating

License

Notifications You must be signed in to change notification settings

mcanthony/Nuke

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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()
  1. Requirements
  2. Getting Started
  3. Usage
  4. Design
  5. Installation
  6. Satellite Projects

Features

Loading
  • 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
Caching
Processing
  • 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+

Getting Started

  • Get a demo project using pod try Nuke command
  • Experiment with Nuke in a playground
  • Install, import Nuke and enjoy!

Usage

Zero Config

Nuke.taskWithURL(imageURL) {
    let image = $0.image
}.resume()

Adding Request Options

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()

Using Image Response

Nuke.taskWithRequest(request) { response in
    switch response {
    case let .Success(image, info): 
        // Use image and inspect info
    case let .Failure(error): 
        // Handle error
    }
}.resume()

Using Image Task

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()

Using UI Extensions

let imageView = UIImageView()
// let task = imageView.nk_setImageWithURL(<#NSURL#>)
let task = imageView.nk_setImageWithRequest(<#ImageRequest#>, options: <#ImageViewLoadingOptions?#>)

Adding UI Extensions

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 }
    }
}

UICollectionView

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()
}

Applying Filters

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()

Composing Filters

let processor1: ImageProcessing = <#processor#>
let processor2: ImageProcessing = <#processor#>
let composition = ImageProcessorComposition(processors: [processor1, processor2])

Preheating Images

let requests = [ImageRequest(URL: imageURL1), ImageRequest(URL: imageURL2)]
Nuke.startPreheatingImages(requests: requests)
Nuke.stopPreheatingImages(requests: requests)

Automate Preheating

let preheater = ImagePreheatingControllerForCollectionView(collectionView: <#collectionView#>)
preheater.delegate = self // Signals when preheat window changes

Customizing Image Manager

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)

Design

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

Installation

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 "kean/Nuke-AnimatedImage-Plugin" # optional

Import

Import installed modules in your source files

import Nuke
import NukeAlamofirePlugin
import NukeAnimatedImagePlugin

Satellite Projects

Contacts

License

Nuke is available under the MIT license. See the LICENSE file for more info.

About

Image loading, processing, caching and preheating

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Swift 98.3%
  • Other 1.7%