Swiftpack.co - Package - SDWebImage/SDWebImageWebPCoder

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 8
  • macOS 10.10
  • tvOS 9.0
  • watchOS 2.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])

Encoding

  • Objective-c
// WebP image encoding
UIImage *image;
NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:image 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)
// 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

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.

Github

link
Stars: 62

Used By

Total: 0

Releases

0.6.1 - Fix Progressive Thread Safe && Max Pixel Size - 2020-04-13 05:24:38

Fixes

  • Fix the potential buffer crash issue during progressive animated webp decoding, and avoid extra copy for partial image data #41

Feature

  • Feature WebP Encoding max pixel size (Thumbnail Encoding) #42

0.6.0 - Support Encode File Size Limit && SD 5.7 - 2020-04-07 13:04:22

Features

  • Supports the maxFileSize encoding options, as well as enabling the thread-encoding for speedup #35

Fixes

  • Fix that the max file size encoding need to increase the passes used by libwebp, we choose to use 6 from cwebp command line #36

Project

  • Upgrade the dependency to SDWebImage 5.7 #35
  • Refactory the workspace and test project path #39

Progressive Animated WebP! - 2020-01-18 12:23:01

Features

  • Feature - Supports Progressive Animated WebP (Important) #33
  • The progressive Animated WebP works compatible for SDAnimatedImageView. For normal UIImageView, it will show the static poster image. Previous version does not support this static poster at all.

Fix Progressive Thumbnail Decoding - 2020-01-18 10:35:21

Fixes

  • Fix the issue that thumbnail decoding does not works for progressive webp image #32

Supports Thumbnail Decoding with SDWebImage 5.5.0 - 2020-01-18 04:55:29

Features

  • WebPCoder now supports the thumbnail decoding #31

Fix extra CGContext RAM usage for Static WebP - 2020-01-07 10:55:54

Fixes

  • Fix the issue when WebP is static webp, the SDAnimatedImageCoder protocol implementation still create CGContext, this can reduce RAM usage #30

Project

  • The libwebp dependency for SwiftPM, now bumped to v1.1.0 official version instead of rc-2

Support Swift Package Manager - 2019-12-27 06:51:11

Features

  • Support Swift Package Manager (with libwebp v1.1.0-rc2) #28

Fix warnings - 2019-10-02 09:10:39

Fixes

  • Fix the warning of deprecated API CGColorSpaceCreateWithICCProfile #24

[Important] Fix Animated WebP render issue on SDAnimatedImageView - 2019-09-12 09:53:36

Fixes

  • Recheck the animated canvas calculation logic, fix the issue of that calculation #23
  • Fix the Animated WebP coder the frame blend index calculation issue, he end condition should be only endIndex to match the behavior. #22

Fix little project config on macOS - 2019-06-02 08:05:44

Project

  • Update to fix the wrong product name SDWebImageWebPCoder_macos.framework for macOS on Carthage users #17

Fix modular headers for CocoaPods - 2019-06-02 03:46:49

Project

  • Fix the issue that could not compile SDWebImageWebPCoder when using CocoaPods with use_modular_header! #15

[Important] bugfix for Animated WebP on `SDAnimatedImageView` - 2019-05-15 05:17:58

Fix

  • Fix the logic for Animated WebP images which contains a subsequence blend from the first frame, impact the SDAnimatedImage #14

Compatible for SDWebImage 5.0 - 2019-04-02 14:33:42

Project

  • Upgrade the SDWebImage dependency to ~> SDWebImage 5.0

Fix ICC Profile except RGB - 2019-02-26 09:56:25

Fix

  • Fix the issue when WebP contains the ICC Profile with colorSpace other than RGB, which cause the CGImageCreate failed #13

Fix ICC Profile crash - 2019-02-21 12:49:36

Fix

  • Fix that WebP with custom ICC Profile will randomly crash, because CGColorSpaceCreateWithICCProfile does not copy the ICC data pointer #12

Fix umbrella headers - 2019-02-02 03:42:55

Fix

  • Remove unnecessary CGImage check when encode first frame #10
  • Fix the umbrella headers and modulemap for CocoaPods #11

Animated WebP performance && fix - 2019-01-28 09:39:28

Performance

  • Fix Aniamted WebP render performance issue #7

Fix

  • Fix the leak of vImageConvertor during WebP encoding #8
  • Fix the issue when provide a UIAnimatedImage and encode it into a static WebP failed #9

Upstream dependency update - 2019-01-26 10:56:57

Fix

  • Upadte the CocoaPods && Carthage dependency to SDWebImage 5.0.0-beta4. Which is the release version don't ship with WebP subspec.

Initial Release - 2019-01-25 03:18:05

Feature

  • Supports Static WebP and Animated WebP decoding & encoding
  • Supports Static WebP progressive decoding
  • Supports Animation rendering protocol used by SDWebImage 5.0