GithubHelp home page GithubHelp logo

yangmiemie1116 / sdwebimagewebpcoder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sdwebimage/sdwebimagewebpcoder

0.0 1.0 0.0 1.14 MB

A WebP coder plugin for SDWebImage, use libwebp

License: MIT License

Ruby 2.58% Objective-C 95.47% Swift 1.95%

sdwebimagewebpcoder's Introduction

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.

sdwebimagewebpcoder's People

Contributors

dreampiggy avatar bpoplauschi avatar kinarobin avatar 0x0c avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.