Update the readme about detail coders usage, accept header modification, etc

This commit is contained in:
DreamPiggy 2019-10-16 14:47:32 +08:00
parent df820a080d
commit f17326681a
1 changed files with 69 additions and 9 deletions

View File

@ -37,21 +37,16 @@ github "SDWebImage/SDWebImageWebPCoder"
## Usage ## Usage
### 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).
+ Objective-C + Objective-C
```objective-c ```objective-c
// Add coder // Add coder
SDImageWebPCoder *webPCoder = [SDImageWebPCoder sharedCoder]; SDImageWebPCoder *webPCoder = [SDImageWebPCoder sharedCoder];
[[SDImageCodersManager sharedManager] addCoder:webPCoder]; [[SDImageCodersManager sharedManager] addCoder:webPCoder];
// WebP image loading
UIImageView *imageView;
NSURL *webpURL;
[imageView sd_setImageWithURL:webpURL];
// WebP image encoding
UIImage *image;
NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatWebP options:nil];
``` ```
+ Swift + Swift
@ -60,17 +55,82 @@ NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image fo
// Add coder // Add coder
let WebPCoder = SDImageWebPCoder.shared let WebPCoder = SDImageWebPCoder.shared
SDImageCodersManager.shared.addCoder(WebPCoder) 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
```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
// WebP image loading
UIImageView *imageView;
NSURL *webpURL;
[imageView sd_setImageWithURL:webpURL];
```
+ Swift
```swift
// WebP online image loading // WebP online image loading
let webpURL: URL let webpURL: URL
let imageView: UIImageView let imageView: UIImageView
imageView.sd_setImage(with: webpURL) imageView.sd_setImage(with: webpURL)
```
### 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
// WebP image encoding // WebP image encoding
let image: UIImage let image: UIImage
let webpData = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: nil) let webpData = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: nil)
``` ```
See more documentation in [SDWebImage Wiki - Coders](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#custom-coder-420)
## Example ## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first. To run the example project, clone the repo, and run `pod install` from the Example directory first.