13

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 (e.g. 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.

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Soonts
  • 20,079
  • 9
  • 57
  • 130

6 Answers6

5

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

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

Shmidt
  • 16,436
  • 18
  • 88
  • 136
  • I've tried implementing this to obtain WebP from a UIImage, and it just hangs on an iPhone 4S running iOS 7: the CPU goes above 90% and just sits there. I linked to a rebuild WebP library with the same result. It hangs during the call to WebPEncodeRGBA. Have you been able to successfully encode a WebP from a UIImage? – pickwick Jan 22 '14 at 19:00
  • @pickwick I didn't tried it on the device, however you can try version with updated framework: https://github.com/seanooi/iOS-WebP – Shmidt Jan 22 '14 at 20:15
  • 1
    I updated the framework. The problems turns out to be my patience: it's just way too slow and processor intensive to encode large images. If I resize the image to something like 1200 x 1200, I can get the data after about 10 seconds. The file size is smaller than a JPEG, but the processing time is just too long for this to be useful in my app. thanks – pickwick Jan 22 '14 at 20:38
  • 1
    It takes 18 seconds to encode a 4K picture on 6S. Most of that time is spent in google's WebPEncodeRGBA – Anton Tropashko Dec 30 '15 at 10:29
  • One way to go is to check if you can ditch google framework and see if you can utilize gpu for encoding. But that's obviously not a one liner effort ;-) – Anton Tropashko Dec 31 '15 at 07:28
1

There is an example . https://github.com/carsonmcdonald/WebP-iOS-example

Nathan Bao
  • 51
  • 1
  • 7
  • Did you read the question, especially "it’s not hard to decode the WebP into RGBA8888 as needed using the google’s C-library"? Of course it's trivial to decode. My question is about the integration of the WebP decoder into the UIKit, not about the decoding itself. – Soonts Jul 10 '12 at 02:57
1

WebP Image Support Coming to iOS 14

update your devices.

Haseeb Javed
  • 1,769
  • 17
  • 20
0

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

nyteshade
  • 2,712
  • 1
  • 14
  • 9
0

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.

MoDJ
  • 4,309
  • 2
  • 30
  • 65
0

SDWebImage is a library which provides an async image downloader which can easily be extended to support WebP via SDWebImageWebPCoder.

Cristan
  • 12,083
  • 7
  • 65
  • 69