Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A WebP coder plugin for SDWebImage, use libwebp

License

NotificationsYou must be signed in to change notification settings

SDWebImage/SDWebImageWebPCoder

Repository files navigation

CI StatusVersionLicensePlatformSwiftPM compatibleCarthage compatiblecodecov

Starting with the SDWebImage 5.0 version, we moved the WebP support code andlibwebp from the Core Repo to this stand-alone repo.

SDWebImageWebPCoder supports both WebP decoding and encoding, for Static WebP or Animated WebP as well.

Note: Apple's ImageIO supports WebP decoding from iOS 14/tvOS 14/watchOS 7/macOS 11, so SDWebImage on those platform can also decode WebP images (usingSDWebImageAWebPCoder built-in coder). However it may contains some limitation, checkSDWebImage/SDWebImage#3558, you can still force to use this coder on those platforms by adding this coder.

Requirements

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

Installation

CocoaPods

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

pod'SDWebImageWebPCoder'

Carthage

SDWebImageWebPCoder is available throughCarthage.

github "SDWebImage/SDWebImageWebPCoder"

Swift Package Manager (Xcode 11+)

SDWebImageWebPCoder is available throughSwift Package Manager.

letpackage=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 coderSDImageWebPCoder *webPCoder = [SDImageWebPCodersharedCoder];[[SDImageCodersManagersharedManager]addCoder:webPCoder];
  • Swift
// Add coderletWebPCoder=SDImageWebPCoder.sharedSDImageCodersManager.shared.addCoder(WebPCoder)

Modify HTTP Accept Header

Some of image server provider may try to detect the client supported format, by default, SDWebImage useimage/*,*/*;q=0.8 forAccept. You can modify it with theimage/webp as well.

  • Objective-C
[[SDWebImageDownloadersharedDownloader]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 loadingNSURL *webpURL;UIImageView *imageView;[imageViewsd_setImageWithURL:webpURL];
  • Swift
// WebP online image loadingletwebpURL:URLletimageView:UIImageViewimageView.sd_setImage(with: webpURL)

Progressive Animation Loading (0.5.0+)

  • Objective-C
// WebP progressive loading for animated imageNSURL *webpURL;SDAnimatedImageView *imageView;imageView.shouldIncrementalLoad =YES;[imageViewsd_setImageWithURL:webpURLplaceholderImage:niloptions:SDWebImageProgressiveLoad];
  • Swift
// WebP progressive loading for animated imageletwebpURL:URLletimageView:SDAnimatedImageViewimageView.shouldIncrementalLoad=trueimageView.sd_setImage(with: webpURL, placeholderImage:nil, options:[.progressiveLoad])

Decoding

  • Objective-C
// WebP image decodingNSData *webpData;UIImage *image = [[SDImageWebPCodersharedCoder]decodedImageWithData:webpDataoptions:nil];
  • Swift
// WebP image decodingletwebpData:Dataletimage=SDImageWebPCoder.shared.decodedImage(with: data, options:nil)

Thumbnail Decoding (0.4.0+)

  • Objective-C
// WebP thumbnail image decodingNSData *webpData;CGSize thumbnailSize = CGSizeMake(300,300);UIImage *thumbnailImage = [[SDImageWebPCodersharedCoder]decodedImageWithData:webpDataoptions:@{SDImageCoderDecodeThumbnailPixelSize : @(thumbnailSize)}];
  • Swift
// WebP thumbnail image decodingletwebpData:DataletthumbnailSize=CGSize(width:300, height:300)letimage=SDImageWebPCoder.shared.decodedImage(with: data, options:[.decodeThumbnailPixelSize: thumbnailSize])

Decoding with limit bytes (0.12.0+)

  • Objective-C
// WebP thumbnail image decodingNSData *webpData;NSUInteger limitBytes =1024 *1024;// 1MBUIImage *image = [[SDImageWebPCodersharedCoder]decodedImageWithData:webpDataoptions:@{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 decodingletwebpData:DataletlimitBytes=1024*1024 // 1MBletimage=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 encodingUIImage *image;NSData *webpData = [[SDImageWebPCodersharedCoder]encodedDataWithImage:imageformat:SDImageFormatWebPoptions:nil];// Animated encodingNSArray<SDImageFrames *> *frames;NSData *awebpData = [[SDImageWebPCodersharedCoder]encodedDataWithFrames:framesloopCount:0format:SDImageFormatWebPoptions:nil];// Encode QualityNSData *lossyWebpData = [[SDImageWebPCodersharedCoder]encodedDataWithImage:imageformat:SDImageFormatWebPoptions:@{SDImageCoderEncodeCompressionQuality : @(0.1)}];// [0, 1] compression qualityNSData *limitedWebpData = [[SDImageWebPCodersharedCoder]encodedDataWithImage:imageformat:SDImageFormatWebPoptions:@{SDImageCoderEncodeMaxFileSize : @(1024 *10)}];// v0.6.0 feature, limit output file size <= 10KB
  • Swift
// WebP image encodingletimage:UIImageletwebpData=SDImageWebPCoder.shared.encodedData(with: image, format:.webP, options:nil)// Animated encodingletframes:[SDImageFrame]letawebpData=SDImageWebPCoder.shared.encodedData(with: frames, loopCount:0, format:.webP, options:nil)// Encode QualityletlossyWebpData=SDImageWebPCoder.shared.encodedData(with: image, format:.webP, options:[.encodeCompressionQuality:0.1]) // [0, 1] compression qualityletlimitedWebpData=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 encodingUIImage *image;NSData *thumbnailWebpData = [[SDImageWebPCodersharedCoder]encodedDataWithImage:imageformat:SDImageFormatWebPoptions:@{SDImageCoderEncodeMaxPixelSize : @(CGSizeMake(200,200))}];// v0.6.1 feature, encoding max pixel size
  • Swift
// WebP image thumbnail encodingletimage:UIImageletthumbnailWebpData=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 inSDWebImage Wiki - Coders

Animated WebP Encoding (0.10+)

  • Objective-c
// Animated encodingNSMutableArray<SDImageFrames *> *frames = [NSMutableArrayarray];for (size_t i =0; i < images.count; i++) {    SDImageFrame *frame = [SDImageFrameframeWithImage:images[i]duration:0.1];    [framesappendObject:frame];}NSData *awebpData = [[SDImageWebPCodersharedCoder]encodedDataWithFrames:framesloopCount:0format:SDImageFormatWebPoptions:nil];
  • Swift
// Animated encodingvarframes:[SDImageFrame]=[]foriin0..<images.count{letframe=SDImageFrame(image:images[i], duration:0.1)    frames.append(frame)}letawebpData=SDImageWebPCoder.shared.encodedData(with: frames, loopCount:0, format:.webP, options:nil)

Advanced WebP codec options (0.8+)

The WebP codeclibwebp 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.sharedCoderencodedDataWithImage:imageformat:SDImageFormatWebPoptions:options];// Will translate into:// config->method = 0;// config->alpha_quality = 100;
  • Swift
letimage:UIImageletoptions=[.encodeWebPMethod:0,.encodeWebPAlphaCompression:100]letdata=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 runpod install from the root directory first. Then openSDWebImageWebPCoder.xcworkspace.

This is a demo to show how to useWebP and animatedWebP images viaSDWebImageWebPCoderExample target.

Screenshot

These WebP images are fromWebP Gallery andGIF vs APNG vs WebP

Author

Bogdan PoplauschiDreamPiggy

License

SDWebImageWebPCoder is available under the MIT license. Seethe LICENSE file for more info.

About

A WebP coder plugin for SDWebImage, use libwebp

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors9


[8]ページ先頭

©2009-2025 Movatter.jp