Skip to content
/ Nuke Public
forked from kean/Nuke

Advanced framework for loading and caching images

License

Notifications You must be signed in to change notification settings

carabina/Nuke

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advanced pure Swift framework for loading, caching, processing, displaying and preheating images. It uses latest advancements in iOS SDK and doesn't reinvent existing technologies. It has an elegant and powerful API that will extend the capabilities of your app.

let URL = NSURL(string: "http://farm8.staticflickr.com/7315/16455839655_7d6deb1ebf_z_d.jpg")!
let task = ImageManager.shared().taskWithURL(URL) {
    let image = $0.image
}
task.resume()

Nuke is a pipeline that loads images using pluggable components which can be injected in runtime.

Programming in Objective-C? Try DFImageManager.

  1. Getting Started
  2. Usage
  3. Design
  4. Installation
  5. Requirements
  6. Contribution

Features

  • Zero config, yet immense customization and flexibility
  • Great performance even on outdated devices, asynchronous and thread safe
Loading
Caching
  • Instead of reinventing a caching methodology it relies on HTTP cache as defined in HTTP specification and caching implementation provided by Foundation
  • Caching is completely transparent to the client
  • Two cache layers, including top level memory cache for decompressed images
Decoding and Processing
  • Background image decompression and scaling in a single step
  • Scale large images (~6000x4000 px) and prepare them for display with ease
  • Resize and crop loaded images to fit displayed size
Advanced
  • Customize different parts of the framework using dependency injection

Getting Started

  • Download the latest release version
  • Experiment with Nuke APIs in a Swift playground
  • Take a look at the demo project, it's easy to install with pod try Nuke command
  • Install using CocoaPods and enjoy!

Usage

Zero Config Image Loading

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

Adding Request Options

var request = ImageRequest(URL: imageURL)
request.targetSize = CGSize(width: 300.0, height: 400.0) // Set target size in pixels
request.contentMode = .AspectFill

ImageManager.shared().taskWithRequest(request) {
    let image = $0.image
}.resume()

Using Image Response

ImageManager.shared().taskWithRequest(request) {
    (response) -> Void in
    switch response { // Response is an enum with associated values
    case let .Success(image, info):
        // Use image and inspect info
    case let .Failure(error):
        // Handle error
    }
}.resume()

Using Image Task

let task = ImageManager.shared().taskWithURL(imageURL) {
    let image = $0.image
}
task.resume()

// Use progress object to track load progress
let progress = task.progress

// Track task state
let state = task.state

// Cancel image task
task.cancel()

Using UI Components

let imageView: ImageView = <#view#>
imageView.setImageWithURL(imageURL)

UICollectionView

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellReuseID, forIndexPath: indexPath)

    let imageView: ImageView = <#view#>
    imageView.prepareForReuse()
    imageView.setImageWithURL(imageURL)

    return cell
}

Cancel image task as soon as the cell goes offscreen (optional):

override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    let imageView: ImageView = <#view#>
    imageView.prepareForReuse()
}

Preheating Images

let requests = [ImageRequest(URL: imageURL1), ImageRequest(URL: imageURL2)]
ImageManager.shared().startPreheatingImages(requests: requests)

ImageManager.shared().stopPreheatingImages(requests: requests)

Customizing Image Manager

let dataLoader: ImageDataLoading = <#data_loader#>
let decoder: ImageDecoding = <#decoder#>
let processor: ImageProcessing = <#processor#>
let cache: ImageMemoryCaching = <#cache#>

let configuration = ImageManagerConfiguration(dataLoader: dataLoader, decoder: decoder, cache: cache, processor: processor)
let manager = ImageManager(configuration: configuration)

Design

Protocol Description
ImageManager A high-level API for loading 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 using CocoaPods

CocoaPods is the dependency manager for Cocoa projects. If you are not familiar with CocoaPods the best place to start would be official CocoaPods guides. To install Nuke add a dependency in your Podfile:

# Podfile
# platform :ios, '8.0'
pod 'Nuke'

Requirements

  • iOS 8.0+
  • Xcode 7.0+, Swift 2.0+

Contribution

  • If you need help, use Stack Overflow. (Tag 'iosnuke')
  • If you'd like to ask a general question, use Stack Overflow.
  • If you found a bug, and can provide steps to reproduce it, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, branch of the develop branch and submit a pull request.

Contacts

License

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

About

Advanced framework for loading and caching images

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.1%
  • Ruby 1.2%
  • Objective-C 0.7%