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).
// 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. You can modify it with the image/webp as well.
// WebP thumbnail image decoding
let webpData: Data
let thumbnailSize = CGSize(width: 300, height: 300)
let image = SDImageWebPCoder.shared.decodedImage(with: data, options: [.decodeThumbnailPixelSize: thumbnailSize])
Decoding with limit bytes (0.12.0+)
Objective-C
// WebP thumbnail image decoding
NSData *webpData;
NSUInteger limitBytes = 1024 * 1024; // 1MB
UIImage *image = [[SDImageWebPCoder sharedCoder] decodedImageWithData:webpData options:@{SDImageCoderDecodeScaleDownLimitBytes : @(limitBytes)}];
// The image pixel buffer is guaranteed to less than 1MB in RAM (may scale down or full size), suitable for large image
Swift
// WebP thumbnail image decoding
let webpData: Data
let limitBytes = 1024 * 1024 // 1MB
let image = SDImageWebPCoder.shared.decodedImage(with: data, options: [.decodeScaleDownLimitBytes: limitBytes])
// The image pixel buffer is guaranteed to less than 1MB in RAM (may scale down or full size), suitable for large image
// Animated encoding
var frames: [SDImageFrame] = []
for i in 0..<images.count {
let frame = SDImageFrame(image: images[i], duration: 0.1)
frames.append(frame)
}
let awebpData = SDImageWebPCoder.shared.encodedData(with: frames, loopCount: 0, format: .webP, options: nil)
Advanced WebP codec options (0.8+)
The WebP codec libwebp we use, supports some advanced control options for encoding/decoding. You can pass them to libwebp by using the wrapper top level API:
SDWebImageWebPCoder
Starting with the SDWebImage 5.0 version, we moved the WebP support code and 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.
Requirements
Installation
CocoaPods
SDWebImageWebPCoder is available through CocoaPods. To install it, simply add the following line to your Podfile:
Carthage
SDWebImageWebPCoder is available through Carthage.
Swift Package Manager (Xcode 11+)
SDWebImageWebPCoder is available through Swift Package Manager.
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).
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. You can modify it with theimage/webp
as well.Loading
Progressive Animation Loading (0.5.0+)
Decoding
Thumbnail Decoding (0.4.0+)
Decoding with limit bytes (0.12.0+)
Encoding
Thumbnail Encoding (0.6.1+)
See more documentation in SDWebImage Wiki - Coders
Animated WebP Encoding (0.10+)
Advanced WebP codec options (0.8+)
The WebP codec libwebp we use, supports some advanced control options for encoding/decoding. You can pass them to libwebp by using the wrapper top level API:
Example
To run the example project, clone the repo, and run
pod install
from the root directory first. Then openSDWebImageWebPCoder.xcworkspace
.This is a demo to show how to use
WebP
and animatedWebP
images viaSDWebImageWebPCoderExample
target.Screenshot
These WebP images are from WebP Gallery and GIF vs APNG vs WebP
Author
Bogdan Poplauschi DreamPiggy
License
SDWebImageWebPCoder is available under the MIT license. See the LICENSE file for more info.