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)
[![Carthage compatible ](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat )](https://github.com/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
```
## 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
2019-01-28 00:00:34 +08:00
// WebP image loading
2018-08-28 23:21:20 +08:00
UIImageView *imageView;
2019-01-28 00:00:34 +08:00
NSURL *webpURL;
[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
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)
```
### Encoding
+ Objective-c
```objective-c
// WebP image encoding
UIImage *image;
NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatWebP options:nil];
```
+ 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)
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
To run the example project, clone the repo, and run `pod install` from the Example directory first.
This is a demo to show how to use `WebP` and animated `WebP` images via `SDWebImage` .
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