diff --git a/README.md b/README.md index 8d67ed4ad..443220c91 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ A powerful **image loading** and **caching** framework which allows for hassle-f # Features - Load images into image views and other targets -- Image resizing, custom image transformations - Two [cache layers](https://kean.github.io/blog/image-caching), fast LRU memory cache - [Alamofire](https://github.com/kean/Nuke-Alamofire-Plugin), [Gifu](https://github.com/kean/Nuke-Gifu-Plugin), [Toucan](https://github.com/kean/Nuke-Toucan-Plugin) plugins - [Freedom to use](#h_design) networking, caching libraries of your choice @@ -96,26 +95,9 @@ Nuke.loadImage(with: request, into: imageView) ``` -#### Resizing Images - -By resizing images to fit/fill the size of the image views you can reduce memory usage and improve drawing performance. Nuke provides a convenience API to do just that: - -```swift -var request = Request(url: url) - -// Use current image view size as a target size for a loaded image. -// By default, Nuke will resize the image to fill (`.aspectFill`) the image view. -// Image won't be resized if it's smaller than the target size. -request.resize(for: imageView) - -// As an alternative you can provide a target size (in pixels) yourself. -request.resize(to: CGSize(width: 150, height: 150), mode: .aspectFit) -``` - - #### Processing Images -You can specify custom image processors using `Processing` protocol which consists of a single method `process(image: Image) -> Image?`: +Nuke provides an infrastructure for processing images and caching them. You can specify custom image processors using `Processing` protocol which consists of a single method `process(image: Image) -> Image?`: ```swift struct GaussianBlur: Processing { @@ -126,41 +108,34 @@ struct GaussianBlur: Processing { } // `Processing` protocol requires `Equatable` to identify cached images. - // If your processor doesn't have any parameters simply return `true`. func ==(lhs: GaussianBlur, rhs: GaussianBlur) -> Bool { - return lhs.radius == rhs.radius + return lhs.radius == rhs.radius // If the processor has no parameters, simply return true } } -``` -> See [Toucan Plugin](https://github.com/kean/Nuke-Toucan-Plugin) for some useful image transformations +// Usage: +let request = Request(url: url).processed(with: GaussianBlur()) +Nuke.loadImage(with: request, into: imageView) +``` > See [Core Image Integration Guide](https://github.com/kean/Nuke/blob/master/Documentation/Guides/Core%20Image%20Integration%20Guide.md) for more info about using Core Image with Nuke -#### Preheating Images +#### Using Toucan Plugin -[Preheating](https://kean.github.io/blog/image-preheating) (prefetching) means loading images ahead of time in anticipation of its use. Nuke provides a `Preheater` class that does just that: +Check out [Toucan Plugin](https://github.com/kean/Nuke-Toucan-Plugin) for some useful image transformations. [Toucan](https://github.com/gavinbunney/Toucan) is a library that provides a clean API for processing images, including resizing, elliptical and rounded rect masking, and more: ```swift -let preheater = Preheater(manager: Manager.shared) - -// User enters the screen: -let requests = [Request(url: url1), Request(url: url2), ...] -preheater.startPreheating(for: requests) - -// User leaves the screen: -preheater.stopPreheating(for: requests) +let request = Nuke.Request(url: url).processed(key: "Avatar") { + return $0.resize(CGSize(width: 500, height: 500), fitMode: .crop) + .maskWithEllipse() +} ``` -You can use Nuke in combination with [Preheat](https://github.com/kean/Preheat) library which automates preheating of content in `UICollectionView` and `UITableView`. With iOS 10.0 you might want to use new [prefetching APIs](https://developer.apple.com/reference/uikit/uitableviewdatasourceprefetching) provided by iOS. - -> See [Performance Guide](https://github.com/kean/Nuke/blob/master/Documentation/Guides/Performance%20Guide.md) to see what else you can do to improve performance - #### Loading Images w/o Targets -You can use `Manager` to load images directly without providing a target. +You can also use `Manager` to load images directly without providing a target. ```swift Manager.shared.loadImage(with: url) { @@ -178,6 +153,7 @@ Manager.shared.loadImage(with: url, token: cts.token) { cts.cancel() ``` + #### Using Memory Cache You can get a directly access to the default memory cache used by Nuke: @@ -192,6 +168,26 @@ let image = Cache.shared[request] ``` +#### Preheating Images + +[Preheating](https://kean.github.io/blog/image-preheating) (prefetching) means loading images ahead of time in anticipation of its use. Nuke provides a `Preheater` class that does just that: + +```swift +let preheater = Preheater(manager: Manager.shared) + +// 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](https://github.com/kean/Preheat) library which automates preheating of content in `UICollectionView` and `UITableView`. With iOS 10.0 you might want to use new [prefetching APIs](https://developer.apple.com/reference/uikit/uitableviewdatasourceprefetching) provided by iOS. + +> See [Performance Guide](https://github.com/kean/Nuke/blob/master/Documentation/Guides/Performance%20Guide.md) to see what else you can do to improve performance + + # Plugins ### [Alamofire Plugin](https://github.com/kean/Nuke-Alamofire-Plugin) diff --git a/Sources/Request.swift b/Sources/Request.swift index bd3585a09..8e590ff78 100644 --- a/Sources/Request.swift +++ b/Sources/Request.swift @@ -140,40 +140,6 @@ public extension Request { } } -#if !os(macOS) -import UIKit - -public extension Request { - /// Resizes the loaded image to *fill* (or *fit* depending on the `mode`) - /// the given target size. - /// - /// This method replaces the current request's processor. If you want to add - /// more processors to the request add them after calling this method. - /// - /// - parameter targetSize: Size in pixels. `MaximumSize` by default. - /// - parameter mode: An option for how to resize the image to the target - /// size. `.aspectFill` by default. - public mutating func resize(to targetSize: CGSize, mode: Decompressor.ContentMode = .aspectFill) { - self.processor = AnyProcessor(Decompressor(targetSize: targetSize, contentMode: mode)) - } - - #if !os(watchOS) - /// Resizes the loaded image to *fill* (or *fit* depending on the `mode`) - /// the given target. Uses the current size of the target. - /// - /// This method replaces the current request's processor. If you want to add - /// more processors to the request add them after calling this method. - /// - /// - parameter target: View for which the image should be resized. - /// - parameter mode: An option for how to resize the image to the target - /// size. `.aspectFill` by default. - public mutating func resize(for target: UIView, mode: Decompressor.ContentMode = .aspectFill) { - resize(to: Decompressor.targetSize(for: target), mode: mode) - } - #endif -} -#endif - public extension Request { /// Returns a key which compares requests with regards to caching images. /// Returns `cacheKey` if not `nil`. Returns default key otherwise.