Skip to content

Commit

Permalink
Remove new resizing API from Request; Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Feb 23, 2017
1 parent dffbdc3 commit 4510b81
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 72 deletions.
72 changes: 34 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ A powerful **image loading** and **caching** framework which allows for hassle-f
# <a name="h_features"></a>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
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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:
Expand All @@ -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<a name="h_plugins"></a>

### [Alamofire Plugin](https://github.com/kean/Nuke-Alamofire-Plugin)
Expand Down
34 changes: 0 additions & 34 deletions Sources/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4510b81

Please sign in to comment.