• Stars
    star
    195
  • Rank 199,374 (Top 4 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created about 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A WebP coder plugin for SDWebImage, use libwebp

SDWebImageWebPCoder

CI Status Version License Platform SwiftPM compatible Carthage compatible codecov

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

  • iOS 9.0
  • macOS 10.11
  • tvOS 9.0
  • watchOS 2.0
  • Xcode 11.0

Installation

CocoaPods

SDWebImageWebPCoder is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SDWebImageWebPCoder'

Carthage

SDWebImageWebPCoder is available through Carthage.

github "SDWebImage/SDWebImageWebPCoder"

Swift Package Manager (Xcode 11+)

SDWebImageWebPCoder is available through Swift Package Manager.

let package = Package(
    dependencies: [
        .package(url: "https://github.com/SDWebImage/SDWebImageWebPCoder.git", from: "0.3.0")
    ]
)

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
// Add coder
SDImageWebPCoder *webPCoder = [SDImageWebPCoder sharedCoder];
[[SDImageCodersManager sharedManager] addCoder:webPCoder];
  • 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. You can modify it with the image/webp as well.

  • Objective-C
[[SDWebImageDownloader sharedDownloader] setValue:@"image/webp,image/*,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
  • Swift
SDWebImageDownloader.shared.setValue("image/webp,image/*,*/*;q=0.8", forHTTPHeaderField:"Accept")

Loading

  • Objective-C
// WebP online image loading
NSURL *webpURL;
UIImageView *imageView;
[imageView sd_setImageWithURL:webpURL];
  • Swift
// WebP online image loading
let webpURL: URL
let imageView: UIImageView
imageView.sd_setImage(with: webpURL)

Progressive Animation Loading (0.5.0+)

  • Objective-C
// WebP progressive loading for animated image
NSURL *webpURL;
SDAnimatedImageView *imageView;
imageView.shouldIncrementalLoad = YES;
[imageView sd_setImageWithURL:webpURL placeholderImage:nil options:SDWebImageProgressiveLoad];
  • 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])

Decoding

  • Objective-C
// WebP image decoding
NSData *webpData;
UIImage *image = [[SDImageWebPCoder sharedCoder] decodedImageWithData:webpData options:nil];
  • Swift
// WebP image decoding
let webpData: Data
let image = SDImageWebPCoder.shared.decodedImage(with: data, options: nil)

Thumbnail Decoding (0.4.0+)

  • Objective-C
// WebP thumbnail image decoding
NSData *webpData;
CGSize thumbnailSize = CGSizeMake(300, 300);
UIImage *thumbnailImage = [[SDImageWebPCoder sharedCoder] decodedImageWithData:webpData options:@{SDImageCoderDecodeThumbnailPixelSize : @(thumbnailSize)}];
  • 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])

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

Encoding

  • Objective-c
// WebP image encoding
UIImage *image;
NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatWebP options:nil];
// Animated encoding
NSArray<SDImageFrames *> *frames;
NSData *awebpData = [[SDImageWebPCoder sharedCoder] encodedDataWithFrames:frames loopCount:0 format:SDImageFormatWebP options:nil];
// 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
  • Swift
// WebP image encoding
let image: UIImage
let webpData = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: nil)
// Animated encoding
let frames: [SDImageFrame]
let awebpData = SDImageWebPCoder.shared.encodedData(with: frames, loopCount: 0, format: .webP, options: nil)
// Encode Quality
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

Thumbnail Encoding (0.6.1+)

  • 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
// WebP image thumbnail encoding
let image: UIImage
let thumbnailWebpData = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: [.encodeMaxPixelSize: CGSize(width: 200, height: 200)]) // v0.6.1 feature, encoding max pixel size

See more documentation in SDWebImage Wiki - Coders

Animated WebP Encoding (0.10+)

  • Objective-c
// Animated encoding
NSMutableArray<SDImageFrames *> *frames = [NSMutableArray array];
for (size_t i = 0; i < images.count; i++) {
    SDImageFrame *frame = [SDImageFrame frameWithImage:images[i] duration:0.1];
    [frames appendObject:frame];
}
NSData *awebpData = [[SDImageWebPCoder sharedCoder] encodedDataWithFrames:frames loopCount:0 format:SDImageFormatWebP options:nil];
  • Swift
// 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:

  • Objective-C
UIImage *image;
SDImageCoderOptions *options = @{SDImageCoderEncodeWebPMethod: @(0), SDImageCoderEncodeWebPAlphaCompression: @(100)};
NSData *data = [SDImageWebPCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatWebP options:options];
// Will translate into:
// config->method = 0;
// config->alpha_quality = 100;
  • Swift
let image: UIImage
let options = [.encodeWebPMethod: 0, .encodeWebPAlphaCompression: 100]
let data = SDImageWebPCoder.shared.encodedData(with: image, format: .webP, options: options)
// Will translate into:
// config->method = 0;
// config->alpha_quality = 100;

Example

To run the example project, clone the repo, and run pod install from the root directory first. Then open SDWebImageWebPCoder.xcworkspace.

This is a demo to show how to use WebP and animated WebP images via SDWebImageWebPCoderExample 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.

More Repositories

1

SDWebImage

Asynchronous image downloader with cache support as a UIImageView category
Objective-C
25,025
star
2

SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
Swift
1,893
star
3

SDWebImageSVGCoder

A SVG coder plugin for SDWebImage, using Apple's built-in framework
Objective-C
92
star
4

SDWebImageAVIFCoder

A SDWebImage coder plugin to support AVIF(AV1 Image File Format) image
Objective-C
55
star
5

SDWebImagePhotosPlugin

A SDWebImage plugin to support Photos framework image loading
Objective-C
54
star
6

libwebp-Xcode

A wrapper for libwebp + Xcode project. Support Carthage && CocoaPods && SwiftPM.
Ruby
53
star
7

SDWebImageLottieCoder

A Lottie animation coder which use SDAnimatedImageView instead of LOTAnimationView for bitmap rendering
Objective-C
40
star
8

SDWebImageHEIFCoder

A SDWebImage coder plugin to support HEIF image without Apple's Image/IO framework
Objective-C
33
star
9

SDWebImageFLPlugin

A SDWebImage plugin to support GIF using FLAnimatedImage and category
Objective-C
25
star
10

SDWebImagePDFCoder

A PDF coder plugin for SDWebImage, using Apple's built-in framework
Objective-C
25
star
11

SDWebImageYYPlugin

A SDWebImage plugin to integrate YYImage & YYCache for image rendering & caching
Objective-C
23
star
12

SDWebImageLottiePlugin

SDWebImage integration with Lottie Animation using remote JSON files
Swift
18
star
13

SDWebImageLinkPlugin

A SDWebImage loader plugin, to support load rich link image with LinkPresentation framework
Objective-C
16
star
14

SDWebImageSVGKitPlugin

A SDWebImage plugin to support SVG with SVGKit and category
Objective-C
16
star
15

librlottie-Xcode

A wrapper for librlottie + Xcode project. Support Carthage && CocoaPods && SwiftPM.
Objective-C
15
star
16

libavif-Xcode

A wrapper for libavif + Xcode project. Support Carthage && CocoaPods && SwiftPM.
Ruby
13
star
17

libde265-Xcode

A wrapper for libde265 + Xcode project. Support Carthage && CocoaPods && SwiftPM.
Ruby
13
star
18

SDWebImageVideoCoder

A toy (non-production) SDWebImage coder plugin to play Video Format like MP4 on Animated Image View
Objective-C
12
star
19

SDWebImageSVGNativeCoder

SVG-Native (SVG OpenType) image coder plugin for SDWebImage
Objective-C
11
star
20

SDWebImageBPGCoder

A BPG coder plugin for SDWebImage, use libbpg
C
10
star
21

SDWebImageAPNGCoder

APNG Coder for SDWebImage
Objective-C
9
star
22

libheif-Xcode

A wrapper for libheif + Xcode project. Support Carthage && CocoaPods && SwiftPM.
Ruby
8
star
23

SDWebImageFLIFCoder

A FLIF coder plugin for SDWebImage, use libflif
Objective-C
7
star
24

SDWebImagePINPlugin

A SDWebImage plugin to integrate PINCache for custom image caching
Objective-C
6
star
25

libdav1d-Xcode

A wrapper for libdav1d + Xcode project. Support Carthage && CocoaPods.
C
6
star
26

libaom-Xcode

A wrapper for libaom + Xcode project. Support Carthage && CocoaPods && SwiftPM.
C
5
star
27

librav1e-Xcode

A wrapper for rav1e (Fast AV1 Encoder) + Xcode project. Using pre-built binary to support CocoaPods.
Ruby
4
star
28

sdwebimage.github.io

SDWebImage Documentation
HTML
3
star
29

libflif-Xcode

A wrapper for libflif + Xcode project. Support Carthage && CocoaPods.
Ruby
2
star
30

libvmaf-Xcode

A wrapper for libvmaf + Xcode project. Support Carthage && CocoaPods.
C
2
star
31

SDWebImageProgressiveJPEGDemo

Demo project of using SDWebImage + Concorde for Progressive JPEG support
Swift
2
star
32

libbpg-Xcode

A wrapper for libbpg + Xcode project. Support Carthage && CocoaPods.
Ruby
2
star
33

libx265-Xcode

A wrapper for libx265 + Xcode project. Support Carthage && CocoaPods.
Ruby
1
star
34

SDWebImageJPEGXLCoder

A SDWebImage coder plugin to support JPEG-XL image
1
star
35

SVT-AV1-Xcode

A wrapper for SVT-AV1 + Xcode project. Support SwiftPM && CocoaPods.
Ruby
1
star