A WebP coder plugin for SDWebImage, use libwebp
Go to file
DreamPiggy a14f6eb29c
Merge pull request #28 from SDWebImage/swiftpm
Support Swift Package Manager (with libwebp v1.1.0-rc2)
2019-12-27 14:46:55 +08:00
Example Fix the warning of deprecated API `CGColorSpaceCreateWithICCProfile` 2019-10-02 17:04:50 +08:00
SDWebImageWebPCoder Support SwiftPM 2019-12-27 11:58:24 +08:00
SDWebImageWebPCoder.xcodeproj Fix the warning of deprecated API `CGColorSpaceCreateWithICCProfile` 2019-10-02 17:04:50 +08:00
SDWebImageWebPCoder.xcworkspace Fix the warning of deprecated API `CGColorSpaceCreateWithICCProfile` 2019-10-02 17:04:50 +08:00
SDWebImageWebPCoderTests Fix one test case after SDWebImage 5.3.0 2019-12-27 14:23:36 +08:00
.gitignore Support SwiftPM 2019-12-27 11:58:24 +08:00
.travis.yml Update the Travis-CI script 2019-12-27 12:19:07 +08:00
Cartfile Update the SDWebImage dependency version to 5.0.0 official release 2019-04-02 22:25:19 +08:00
Cartfile.resolved Update the SDWebImage dependency version to 5.0.0 official release 2019-04-02 22:25:19 +08:00
LICENSE Initial checkin of the project - minimal code + docs + Travis CI script 2018-08-28 18:21:20 +03:00
Package.resolved Support SwiftPM 2019-12-27 11:58:24 +08:00
Package.swift Fix the platform limit in SwiftPM 2019-12-27 12:06:31 +08:00
README.md Update the readme 2019-12-27 12:01:48 +08:00
SDWebImageWebPCoder.podspec Bumped version to 0.2.5 2019-10-02 17:09:39 +08:00

README.md

SDWebImageWebPCoder

CI Status Version License Platform SwiftPM compatible Carthage compatible

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 image loading
UIImageView *imageView;
NSURL *webpURL;
[imageView sd_setImageWithURL:webpURL];
  • Swift
// WebP online image loading
let webpURL: URL
let imageView: UIImageView
imageView.sd_setImage(with: webpURL)

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)

Encoding

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

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

This is a demo to show how to use WebP and animated WebP images via SDWebImage.

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.