From f17326681aaef77b63d78ea13b85c946b774ac20 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 16 Oct 2019 14:47:32 +0800 Subject: [PATCH] Update the readme about detail coders usage, accept header modification, etc --- README.md | 78 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b06c8a0..937f156 100644 --- a/README.md +++ b/README.md @@ -37,21 +37,16 @@ github "SDWebImage/SDWebImageWebPCoder" ## 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 // Add coder SDImageWebPCoder *webPCoder = [SDImageWebPCoder sharedCoder]; [[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 @@ -60,17 +55,82 @@ NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image fo // 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 + +```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 let webpURL: URL let imageView: UIImageView 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 let image: UIImage 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 To run the example project, clone the repo, and run `pod install` from the Example directory first.