ainame / Swift-WebP

A thin libwebp wrapper in Swift that provides both encode/decode APIs
MIT License
163 stars 39 forks source link
image-processing libwebp swift swift-wrapper webp

Swift-WebP

Swift-WebP provides libwebp APIs in Swift manner for both encoding and decoding.

Support Versions:

Features

TODO

Usage

Encoding

let image = UIImage(named: "demo")
let encoder = WebPEncoder()
let queue =  DispatchQueue(label: "me.ainam.webp")

// should encode in background
queue.async {
    let data = try! encoder.encode(image, config: .preset(.picture, quality: 95))
    // using webp binary data...
}

Decoding

let data: Data = loadWebPData()
let encoder = WebPDecoder()
let queue =  DispatchQueue(label: "me.ainam.webp")

// should decode in background
queue.async {
    var options = WebPDecoderOptions()
    options.scaledWidth = Int(originalWidth / 2)
    options.scaledHeight = Int(originalHeight / 2)
    let cgImage = try! decoder.decode(data, options: options)
    let webpImage = UIImage(cgImage: cgImage)

    DispatchQueue.main.async {
        self.imageView.image = webpImage
    }
}

Example

Please check example project

Installation

Swift-WebP supports Swift Package Manager installation.

.package(url: "https://github.com/ainame/Swift-WebP.git", from: "0.5.0"),

Author

ainame

License

Swift-WebP is available under the MIT license. See the LICENSE file for more info.