2018-08-29 19:50:53 +08:00
# SDWebImageWebPCoder
2018-08-28 23:21:20 +08:00
2018-08-29 19:50:53 +08:00
[![CI Status ](http://img.shields.io/travis/SDWebImage/SDWebImageWebPCoder.svg?style=flat )](https://travis-ci.org/SDWebImage/SDWebImageWebPCoder)
[![Version ](https://img.shields.io/cocoapods/v/SDWebImageWebPCoder.svg?style=flat )](http://cocoapods.org/pods/SDWebImageWebPCoder)
[![License ](https://img.shields.io/cocoapods/l/SDWebImageWebPCoder.svg?style=flat )](http://cocoapods.org/pods/SDWebImageWebPCoder)
[![Platform ](https://img.shields.io/cocoapods/p/SDWebImageWebPCoder.svg?style=flat )](http://cocoapods.org/pods/SDWebImageWebPCoder)
2019-12-27 12:01:48 +08:00
[![SwiftPM compatible ](https://img.shields.io/badge/SwiftPM-compatible-brightgreen.svg?style=flat )](https://swift.org/package-manager/)
2018-08-29 19:50:53 +08:00
[![Carthage compatible ](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat )](https://github.com/SDWebImage/SDWebImageWebPCoder)
2020-01-27 17:40:26 +08:00
[![codecov ](https://codecov.io/gh/SDWebImage/SDWebImageWebPCoder/branch/master/graph/badge.svg )](https://codecov.io/gh/SDWebImage/SDWebImageWebPCoder)
2018-08-28 23:21:20 +08:00
2019-01-28 00:00:34 +08:00
Starting with the SDWebImage 5.0 version, we moved the WebP support code and [libwebp ](https://github.com/webmproject/libwebp ) from the Core Repo to this stand-alone repo.
SDWebImageWebPCoder supports both WebP decoding and encoding, for Static WebP or Animated WebP as well.
2018-08-28 23:21:20 +08:00
## Requirements
+ iOS 8
+ macOS 10.10
+ tvOS 9.0
+ watchOS 2.0
## Installation
#### CocoaPods
2018-08-29 19:50:53 +08:00
SDWebImageWebPCoder is available through [CocoaPods ](http://cocoapods.org ). To install it, simply add the following line to your Podfile:
2018-08-28 23:21:20 +08:00
```ruby
2018-08-29 19:50:53 +08:00
pod 'SDWebImageWebPCoder'
2018-08-28 23:21:20 +08:00
```
#### Carthage
2018-08-29 19:50:53 +08:00
SDWebImageWebPCoder is available through [Carthage ](https://github.com/Carthage/Carthage ).
2018-08-28 23:21:20 +08:00
```
2018-08-29 19:50:53 +08:00
github "SDWebImage/SDWebImageWebPCoder"
2018-08-28 23:21:20 +08:00
```
2019-12-27 12:01:48 +08:00
#### Swift Package Manager (Xcode 11+)
SDWebImageWebPCoder is available through [Swift Package Manager ](https://swift.org/package-manager ).
```swift
let package = Package(
dependencies: [
.package(url: "https://github.com/SDWebImage/SDWebImageWebPCoder.git", from: "0.3.0")
]
)
```
2018-08-28 23:21:20 +08:00
## Usage
2019-10-16 14:47:32 +08:00
### Add Coder
Before using SDWebImage to load WebP images, you need to register the WebP Coder to your coders manager. This step is recommended to be done after your App launch (like AppDelegate method).
2019-01-28 00:00:34 +08:00
+ Objective-C
2018-08-28 23:21:20 +08:00
```objective-c
2019-01-28 00:00:34 +08:00
// Add coder
2018-08-28 23:21:20 +08:00
SDImageWebPCoder *webPCoder = [SDImageWebPCoder sharedCoder];
2019-01-28 00:00:34 +08:00
[[SDImageCodersManager sharedManager] addCoder:webPCoder];
2019-10-16 14:47:32 +08:00
```
+ Swift
```swift
// Add coder
let WebPCoder = SDImageWebPCoder.shared
SDImageCodersManager.shared.addCoder(WebPCoder)
```
### Modify HTTP Accept Header
Some of image server provider may try to detect the client supported format, by default, SDWebImage use `image/*,*/*;q=0.8` for [Accept ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept ). You can modify it with the `image/webp` as well.
+ Objective-C
2018-08-28 23:21:20 +08:00
2019-10-16 14:47:32 +08:00
```objective-c
[[SDWebImageDownloader sharedDownloader] setValue:@"image/webp,image/*,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
```
+ Swift
```swift
SDWebImageDownloader.shared.setValue("image/webp,image/*,*/*;q=0.8", forHTTPHeaderField:"Accept")
```
### Loading
+ Objective-C
```objective-c
2020-01-18 19:17:08 +08:00
// WebP online image loading
2019-01-28 00:00:34 +08:00
NSURL *webpURL;
2020-01-18 19:17:08 +08:00
UIImageView *imageView;
2019-01-28 00:00:34 +08:00
[imageView sd_setImageWithURL:webpURL];
```
+ Swift
```swift
// WebP online image loading
let webpURL: URL
let imageView: UIImageView
imageView.sd_setImage(with: webpURL)
2019-10-16 14:47:32 +08:00
```
2019-01-28 00:00:34 +08:00
2020-01-18 19:17:08 +08:00
### Progressive Animation Loading (0.5.0+)
+ Objective-C
```objective-c
// WebP progressive loading for animated image
NSURL *webpURL;
SDAnimatedImageView *imageView;
imageView.shouldIncrementalLoad = YES;
[imageView sd_setImageWithURL:webpURL placeholderImage:nil options:SDWebImageProgressiveLoad];
```
+ Swift
```swift
// WebP progressive loading for animated image
let webpURL: URL
let imageView: SDAnimatedImageView
imageView.shouldIncrementalLoad = true
imageView.sd_setImage(with: webpURL, placeholderImage: nil, options: [.progressiveLoad])
```
2019-10-16 14:47:32 +08:00
### Decoding
+ Objective-C
```objective-c
// WebP image decoding
NSData *webpData;
UIImage *image = [[SDImageWebPCoder sharedCoder] decodedImageWithData:webpData options:nil];
```
+ Swift
```swift
// WebP image decoding
let webpData: Data
let image = SDImageWebPCoder.shared.decodedImage(with: data, options: nil)
```
2020-01-18 19:17:08 +08:00
### Thumbnail Decoding (0.4.0+)
2020-01-18 12:41:46 +08:00
+ Objective-C
```objective-c
// WebP thumbnail image decoding
NSData *webpData;
CGSize thumbnailSize = CGSizeMake(300, 300);
UIImage *thumbnailImage = [[SDImageWebPCoder sharedCoder] decodedImageWithData:webpData options:@{SDImageCoderDecodeThumbnailPixelSize : @(thumbnailSize}];
```
+ Swift
```swift
// WebP thumbnail image decoding
let webpData: Data
let thumbnailSize = CGSize(width: 300, height: 300)
let image = SDImageWebPCoder.shared.decodedImage(with: data, options: [.decodeThumbnailPixelSize: thumbnailSize])
```
2019-10-16 14:47:32 +08:00
### Encoding
+ Objective-c
```objective-c
// WebP image encoding
UIImage *image;
NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatWebP options:nil];
2020-04-07 20:59:03 +08:00
// Encode Quality
NSData *lossyWebpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatWebP options:@{SDImageCoderEncodeCompressionQuality : @(0.1)}]; // [0, 1] compression quality
NSData *limitedWebpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatWebP options:@{SDImageCoderEncodeMaxFileSize : @(1024 * 10)}]; // v0.6.0 feature, limit output file size < = 10KB
2019-10-16 14:47:32 +08:00
```
+ Swift
```swift
2019-01-28 00:00:34 +08:00
// WebP image encoding
let image: UIImage
2019-01-28 17:29:41 +08:00
let webpData = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: nil)
2020-04-07 20:59:03 +08:00
let lossyWebpData = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: [.encodeCompressionQuality: 0.1]) // [0, 1] compression quality
let limitedWebpData = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: [.encodeMaxFileSize: 1024 * 10]) // v0.6.0 feature, limit output file size < = 10KB
2020-04-13 13:28:05 +08:00
```
### Thumbnail Encoding (0.6.1+)
+ Objective-C
```objective-c
// WebP image thumbnail encoding
UIImage *image;
NSData *thumbnailWebpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatWebP options:@{SDImageCoderEncodeMaxPixelSize : @(CGSizeMake(200, 200)}]; // v0.6.1 feature, encoding max pixel size
```
+ Swift
```swift
// WebP image thumbnail encoding
let image: UIImage
2020-04-13 13:22:30 +08:00
let thumbnailWebpData = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: [.encodeMaxPixelSize: CGSize(width: 200, height: 200)]) // v0.6.1 feature, encoding max pixel size
2018-08-28 23:21:20 +08:00
```
2019-10-16 14:47:32 +08:00
See more documentation in [SDWebImage Wiki - Coders ](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#custom-coder-420 )
2018-08-28 23:21:20 +08:00
## Example
2020-04-07 20:59:03 +08:00
To run the example project, clone the repo, and run `pod install` from the root directory first. Then open `SDWebImageWebPCoder.xcworkspace` .
2018-08-28 23:21:20 +08:00
2020-04-07 20:59:03 +08:00
This is a demo to show how to use `WebP` and animated `WebP` images via `SDWebImageWebPCoderExample` target.
2018-08-28 23:21:20 +08:00
2019-01-28 00:00:34 +08:00
## Screenshot
< img src = "https://raw.githubusercontent.com/SDWebImage/SDWebImageWebPCoder/master/Example/Screenshot/WebPDemo.png" width = "300" / >
These WebP images are from [WebP Gallery ](https://developers.google.com/speed/webp/gallery1 ) and [GIF vs APNG vs WebP ](http://littlesvr.ca/apng/gif_apng_webp.html )
2018-08-28 23:21:20 +08:00
## Author
[Bogdan Poplauschi ](https://github.com/bpoplauschi )
2019-01-28 00:00:34 +08:00
[DreamPiggy ](https://github.com/dreampiggy )
2018-08-28 23:21:20 +08:00
## License
2018-08-29 19:50:53 +08:00
SDWebImageWebPCoder is available under the MIT license. See [the LICENSE file ](https://github.com/SDWebImage/SDWebImageWebPCoder/blob/master/LICENSE ) for more info.
2019-01-28 00:00:34 +08:00