Skip to content
/ Nuke Public
forked from kean/Nuke

Micro-framework for loading, processing, caching and preheating images

License

Notifications You must be signed in to change notification settings

Hitlabs/Nuke

 
 

Repository files navigation

Micro-framework for loading, processing, caching and preheating images.

Features

Getting Started

Usage

Loading Images

Nuke allows for hassle-free image loading into image views (and other arbitrary targets).

Nuke.loadImage(with: URL(string: "http://...")!, into: imageView)

Customizing Requests

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)

Processing Images

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 Images

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)

Automating Preheating

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

Loading Images Directly

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

Design

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

Requirements

  • iOS 9.0 / watchOS 2.0 / macOS 10.11 / tvOS 9.0
  • Xcode 8, Swift 3

Installation

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

License

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

About

Micro-framework for loading, processing, caching and preheating images

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.3%
  • Ruby 1.5%
  • Objective-C 0.2%