iOS 上的 WebP 图片格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8672393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:04:04  来源:igfitidea点击:

WebP image format on iOS

iphoneiosuiimageimage-formatswebp

提问by Soonts

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

我目前正在研究在我们的 iOS 软件中使用 Google 的 WebP 图像格式的可能性。

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

我发现使用 Google 的 C 库根据需要将 WebP 解码为 RGBA8888 并不难。

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

但是,我想创建一个在 API 和性能方面与 UIImage 相当的实现。

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

一季度。是否可以在 iOS 中开发图像解码器,以便本机 imageWithData 和其他 API 可以读取新格式?

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?

Q2。如果不是,UIImageView(和其他框架提供的控件)使用什么 API 来绘制 UIImage?它是公共的(例如 drawInRect/drawAtPoint)还是内部的?

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?

我可以从 UIImage 继承,覆盖一些方法(例如 +imageWithContentsOfFile、+imageWithData、+imageNamed、-drawInRect、-drawAtPoint),并使我的 WPImage 对象与 SDK 提供的 API 表现良好吗?

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?

Q3。如果我假设的 WPImage 类的每个实例都将订阅 UIApplicationDidReceiveMemoryWarningNotification(以刷新 RGBA 图像缓冲区,而在 RAM 中留下小得多的原始 WebP 数据),它会不会对性能造成很大影响?

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

我们正在开发的软件可能很容易在 RAM 中包含数百个不同的图像。

回答by Shmidt

I made full (decode/encode) UIImagewrapper for WebP.

UIImageWebP.

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

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

回答by nyteshade

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:

我写的另一个例子使用了 Carson McDonald 开始的工作,但以可重复使用的方式。基本上,您将 WebP.framework 添加到您的项目中(包含在我的示例中或使用 Carson 在其网站上的说明进行创建),然后您可以执行以下操作:

#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

如果你想看看我的例子,去这里:https: //github.com/nyteshade/iOSWebPWithAlphaExample

回答by MoDJ

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.

首先,将 WebP 像素解码到缓冲区中。然后您需要调用 CGDataProviderCreateWithData() 为 CoreGraphics 创建一个“数据提供者”,然后调用 CGImageCreate() 并传入提供者和所有需要的参数。最后,调用 UIImage imageWithCGImage:imageRef 以创建 UIImage 实例。