Swift-WebP provides libwebp APIs in Swift manner for both encoding and decoding.
- libwebp: v1.2.0
- iOS Deployment Target: 13.0
- macOS Deployment Target: 11.0
- Support mutiplatform; iOS, macOS, and Linux (swift-docker)
- Support SPM
- Advanced Encoder API - WebPEncoder, WebPEncoderConfig
- Advanced Decoding API - WebPDecoder, WebPDecoderOptions
- Image inspection for WebP files - WebPImageInspector
- Progressively encoding/decoding option
- Animated WebP
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...
}
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
}
}
Please check example project
Swift-WebP supports Swift Package Manager installation.
.package(url: "https://github.com/ainame/Swift-WebP.git", from: "0.5.0"),
ainame
Swift-WebP is available under the MIT license. See the LICENSE file for more info.