简体   繁体   中英

WebP image format on iOS

I'm currently researching the possibility to use Google's WebP image format in our iOS software.

I found it's not hard to decode the WebP into RGBA8888 as needed using the Google's C-library.

However, I'd like to create an implementation comparable to UIImage in terms of both API and performance.

Q1. Is it possible in iOS to develop an image decoder so that native imageWithData and other APIs will read the new format?

Q2. If no, what API does UIImageView (and other framework-provided controls) use to draw the UIImage? Is it public (eg drawInRect/drawAtPoint) or internal?

Can I inherit from UIImage, override a few methods (such as +imageWithContentsOfFile, +imageWithData, +imageNamed, -drawInRect, -drawAtPoint), and have my WPImage objects behave well with SDK-provided APIs?

Q3. If every instance of my hypothetical WPImage class will subscribe for UIApplicationDidReceiveMemoryWarningNotification (to flush RGBA image buffer leaving the much smaller original WebP data in RAM), won't it hurt the performance much?

The software we're developing may easily have hundreds of different images in RAM.

I made full (decode/encode) UIImage wrapper for WebP .

https://github.com/shmidt/WebP-UIImage

WebP Image Support Coming to iOS 14

update your devices.

Another example I've written uses the work that Carson McDonald started but in a reusable fashion. Basically you add the WebP.framework to your project (included in my example or create-able using the instructions Carson has on his website) and then you can do something like this:

#import "UIImage+WebP.h"
...

self.imageView.image = [UIImage imageFromWebP:@"path/to/image.webp"];
// or
[self.button setImage:[UIImage imageFromWebP:@"path/to/image.webp" 
        forState:UIControlStateNormal]];

If you want to take a look at my example, go here: https://github.com/nyteshade/iOSWebPWithAlphaExample

First, decode the WebP pixels into a buffer. You then need to call CGDataProviderCreateWithData() to create a "data provider" for CoreGraphics and then call CGImageCreate() and pass in the provider and all the needed arguments. Finally, invoke UIImage imageWithCGImage:imageRef in order to create a UIImage instance.

SDWebImage是一个提供异步图像下载器的库,它可以通过SDWebImageWebPCoder轻松扩展以支持 WebP。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM