由于公司需要,现在要研究一下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>
<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 CGDataProviderRelease static void FreeImageData(void *info, const void *data, size_t size) { free((void*)data); }</span>