WebPKit
is an open source framework that wraps Google's WebP library to provide native Cocoa APIs for working with WebP image files on all of Apple's platforms.
WebPKit
allows for easily decoding WebP files to UIImage
or NSImage
instances. It also includes advanced features such as decoding WebP images to custom sizes, and will later support writing to WebP image files as well.
- Read image files in the WebP format.
- (TODO:) Write in-memory images to the WebP format.
- Supports all of Apple's platforms (including Mac Catalyst).
- Additional convenience methods for quickly verifying WebP data before decoding.
- 100% Swift, and fully unit-tested.
- iOS: 9.0 and above
- macOS: 10.9 and above
- tvOS: 9.0 and above
- watchOS: 2.0 and above
When installing manually, you will also need Google's WebP
C library as well. Precompiled static binaries are available at the Cocoa-WebP repo.
WebPKit
provides extensions to a variety of popular Cocoa classes in order to natively provide WebP format support.
WebPKit
provides a variety of methods to check the contents of a file or some data to see if it is a valid WebP file.
import WebPKit
// Check a `Data` object to see if it contains WebP data
let webpData = Data(...)
print(webpData.isWebP)
// Check a file on disk to see if it is a WebP file
let url = URL(...)
print(url.isWebP)
// Retrieve the pixel size of the image without decoding it
let size = CGImage.sizeOfWebP(with: Data())
WebPKit
performs decoding of WebP image data at the CGImage
level, and then provides convenience initialisers for UIImage
and NSImage
on iOS-based platforms and macOS respectively.
import WebPKit
// Load from data in memory
let webpImage = UIImage(webpData: Data()
// Load from disk
let webpImage = UIImage(contentsOfWebPFile: URL())
// Load from resource bundle
let webpImage = UIImage.webpNamed("MyWebPImage")
import WebPKit
// Load from data in memory
let webpImage = NSImage(webpData: Data()
// Load from disk
let webpImage = NSImage(contentsOfWebPFile: URL())
// Load from resource bundle
let webpImage = NSImage.webpNamed("MyWebPImage")
CocoaPods
Add the following to your Podfile
:
pod 'WebPKit'
Carthage
Carthage support is coming soon. Stay tuned!
Swift Package Manager
SPM support is coming soon. Stay tuned!
Manual Installation
1. Download this repository.
2. Copy the `WebPKit` folder to your Xcode project.
3. Download the precompiled WebP binary from [the Cocoa-WebP repo](https://github.com/TimOliver/WebP-Cocoa) for your desired platform.
4. Drag that framework into your Xcode project.
Support for WebP image files had been a growing feature request in my comic reader app iComics for a number of years. With iComics being in Objective-C, I was able to use one of the many libraries on GitHub out there to easily support it.
But while that was the case for Objective-C, while I've been working on iComics 2, I started to realise that there still wasn't a great amount of WebP support for Apple's more modern platforms and features.
Google's own precompiled binaries don't support either Swift or Mac Catalyst, and all the Swift libraries I found didn't provide features I needed, like CocoaPods support, or decoding at different sizes.
For a feature that will be an extremely fundamental pillar in iComics 2, I decided that it would be worth the time and investment to make a really good WebP framework for the Apple ecosystem.
WebPKit
was created by Tim Oliver.
A huge shout-out also goes to the SDWebImage
team for maintaining CocoaPods and Carthage releases for WebP all this time as well.
WebPKit
is licensed under the MIT License. Please see the LICENSE file for more details.