关于iOS中显示WEBP图像的学习记录

来源:互联网 发布:淘宝网钻石画弥勒佛 编辑:程序博客网 时间:2024/06/07 17:34

由于公司需要,现在要研究一下webp的图片格式,下面是我的一点记录


WebP是Google新推出的影像技术,它可让网页图档有效进行压缩,同时又不影响图片格式兼容与实际清晰度,进而让整体网页下载速度加快。
由于目前互联网上传输的数据有65%都是图片,WebP就是出于减少数据量、加速网络传输的目的而开发的。为了改善JPEG的图片压缩技术,他们使用了一种基于VP8编码(已在2010五月开源)的图片压缩器,利用预测编码技术,同时还采用了一种基于RIFF的非常轻量级的容器。这种容器只会给每张图片增加20字节,但能让图片作者保存他们想要存储的元数据。
与JPEG相同,WebP是一种有损压缩利用预测编码技术。但谷歌表示,这种格式的主要优势在于高效率。他们发现,“在质量相同的情况下,WebP格式图像的体积要比JPEG格式图像小40%。
目前, Google放出了WebP文件解码器(libvpx)和命令行工具(webpconv),用于JPEG等格式图片与WebP格式之间的转换,不过系统支持暂时仅限Linux,Windows版本将在稍后推出。
WebP团队还在开发WebKit内核补丁,用于在Google Chrome浏览器中提供对WebP格式的原生支持。
美中不足的是,WebP格式图像的编码时间“比JPEG格式图像长8倍”。
分析人士认为,尽管WebP格式尚未像JPEG格式那样,得到各种软硬件的广泛支持,但谷歌推广这一格式的优势在于Chrome浏览器。这款谷歌开发的浏览器的市场份额已达10%以上。

在ios里我们可以从github上直接找到相关的代码,这里是连接:iOS-WebP

主要的方法如下
<span style="font-size:18px;">//转换一个img成webp+ (void)imageToWebP:(UIImage *)image quality:(CGFloat)quality alpha:(CGFloat)alpha preset:(WebPPreset)preset    completionBlock:(void (^)(NSData *result))completionBlock       failureBlock:(void (^)(NSError *error))failureBlock;//转换一个webp成img+ (void)imageFromWebP:(NSString *)filePath      completionBlock:(void (^)(UIImage *result))completionBlock         failureBlock:(void (^)(NSError *error))failureBlock;//透明度- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha;</span>

还需要注意的就是他有几个不同的属性,不同的属性压缩出的图片也是有一点点的大小区分
<span style="font-size:18px;">WEBP_PRESET_DEFAULT (default preset)WEBP_PRESET_PICTURE (digital picture, like portrait, inner shot)WEBP_PRESET_PHOTO (outdoor photograph, with natural lighting)WEBP_PRESET_DRAWING (hand or line drawing, with high-contrast details)WEBP_PRESET_ICON (small-sized colorful images)WEBP_PRESET_TEXT (text-like)</span>

-----------------------------------------5.25补充-----------------------------------------
针对某些图片无法解析的问题,下面的代码或许能有所帮助

<span style="font-size:18px;">//5.22 解决本地预加载漫画的问题 wyl    if (imageWebp == nil) {        WebPDecoderConfig config;        if (!WebPInitDecoderConfig(&config)) {            NSLog(@"WebPDecoderConfig error");        }        config.output.colorspace = MODE_RGBA;        config.options.use_threads = 1;        imageWebp = [UIImage decodeWebPFromFile:self.imageData withConfig:&config];        [imageWebp retain];    }</span>

<span style="font-size:18px;">+ (UIImage *)decodeWebPFromFile:(NSData *)myData                     withConfig:(WebPDecoderConfig *)config {        // Decode the WebP image data into a RGBA value array.    if (WebPDecode([myData bytes], [myData length], config) != VP8_STATUS_OK) {        return nil;    }        int width = config->input.width;    int height = config->input.height;    if (config->options.use_scaling) {        width = config->options.scaled_width;        height = config->options.scaled_height;    }        // Construct a UIImage from the decoded RGBA value array.    CGDataProviderRef provider =    CGDataProviderCreateWithData(NULL, config->output.u.RGBA.rgba,                                 config->output.u.RGBA.size, FreeImageData);    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;    CGImageRef imageRef =    CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo,                  provider, NULL, NO, renderingIntent);        CGColorSpaceRelease(colorSpaceRef);    CGDataProviderRelease(provider);        UIImage *newImage = [[UIImage alloc] initWithCGImage:imageRef] ;    CGImageRelease(imageRef);            return newImage;}// Callback for CGDataProviderReleasestatic void FreeImageData(void *info, const void *data, size_t size) {    free((void*)data);}</span>


0 0