iOS优化

webP压缩图片及使用

2020-09-04  本文已影响0人  crazyfox

GUI工具

下载下来后,使用没反应,表示有点蛋疼


111.gif

命令行工具

webp的安装:

brew install webp

使用:

1.压缩

描述:

将WebP文件解压成一个图像文件,类型可以是JPG,PNG,PAM,PPM,PGM等等。

用法:

 dwebp [options] input_file.webp

2.解压

描述:

从静态的webp images创建动态的webp,或者从动态的webp提取静态的webp images。以及管理XMP/EXIF数据和ICC描述文件。

用法:

webpmux -get GET_OPTIONS INPUT -o OUTPUT

webpmux -set SET_OPTIONS INPUT -o OUTPUT

webpmux -strip STRIP_OPTIONS INPUT -o OUTPUT

webpmux -frame FRAME_OPTIONS [ -frame ... ] [ -loop LOOP_COUNT ][ -bgcolor BACKGROUND_COLOR ] -o OUTPUT

webpmux -duration DURATION OPTIONS [ -duration ... ] INPUT -o OUTPUT

webpmux -info INPUT

webpmux [-h|-help]

webpmux -version

参考:https://www.jianshu.com/p/61ab330a6de6

tips:参数不对,比如无损压缩反而大了,解压也更大
原图


截屏2020-09-04下午4.15.31.png

压缩


截屏2020-09-04下午4.15.36.png

解压


截屏2020-09-04下午4.15.40.png

iOS项目里使用

首先需要导入libwebp
pod 'libwebp'

pod install 

时可能会遇到问题Error installing libwebp

解决方案:
1.查看mac中cocoapods 本地库路径

pod repo

2.在本地库中, 并找到对应的libwebp版本的文件

find /Users/swae/.cocoapods/repos/master -iname libwebp
find /Users/swae/.cocoapods/repos/trunk -iname libwebp

3.切换到文件夹

cd /Users/swae/.cocoapods/repos/master/Specs/1/9/2/libwebp

4.编辑json文件,如果不行,master和trunk分支都编辑一下

截屏2020-09-04下午4.21.29.png

5.pod install


截屏2020-09-04下午4.24.07.png
其次使用

demo使用

#import <libwebp/libwebp-umbrella.h>
#pragma mark - WebP example

/*
 This gets called when the UIImage gets collected and frees the underlying image.
 */
static void free_image_data(void *info, const void *data, size_t size)
{
    if(info != NULL)
    {
        WebPFreeDecBuffer(&(((WebPDecoderConfig *)info)->output));
    }
    else
    {
        free((void *)data);
    }
}

- (void)displayImage:(NSString *)filePath
{
    NSLog(@"* * * * * * * * * * *");
    
    NSDate *startTime = [NSDate date];
    
    // Find the path of the selected WebP image in the bundle and read it into memory
    NSData *myData = [NSData dataWithContentsOfFile:filePath];
    
    NSLog(@"Time to read data: %0.2fms", [startTime timeIntervalSinceNow] * -1000.0);
    
    // Get the current version of the WebP decoder
    int rc = WebPGetDecoderVersion();
    
    NSLog(@"Version: %d", rc);
    
    startTime = [NSDate date];
    
    // Get the width and height of the selected WebP image
    int width = 0;
    int height = 0;
    WebPGetInfo([myData bytes], [myData length], &width, &height);
    
    NSLog(@"Time to read info: %0.2fms", [startTime timeIntervalSinceNow] * -1000.0);
    
    NSLog(@"Image Width: %d Image Height: %d", width, height);
    
    CGDataProviderRef provider;
    
    if(_bypassFilteringSwitch.on || _noFancyUpsamplingSwitch.on || _useThreadsSwitch.on )
    {
        WebPInitDecoderConfig(&config);
        
        config.options.no_fancy_upsampling = _noFancyUpsamplingSwitch.on ? 1 : 0;
        config.options.bypass_filtering = _bypassFilteringSwitch.on ? 1 : 0;
        config.options.use_threads = _useThreadsSwitch.on ? 1 : 0;
        config.output.colorspace = MODE_RGBA;
        
        NSLog(@"Settings: no_fancy_upsampling=%d, bypass_filtering=%d, use_threads=%d", config.options.no_fancy_upsampling, config.options.bypass_filtering, config.options.use_threads);
        
        startTime = [NSDate date];
        
        // Decode the WebP image data into a RGBA value array
        WebPDecode([myData bytes], [myData length], &config);
        
        NSLog(@"Time to decode WebP data: %0.2fms", [startTime timeIntervalSinceNow] * -1000.0);
        
        startTime = [NSDate date];
        
        // Construct a UIImage from the decoded RGBA value array
        provider = CGDataProviderCreateWithData(&config, config.output.u.RGBA.rgba, width*height*4, free_image_data);
    }
    else
    {
        startTime = [NSDate date];
        
        // Decode the WebP image data into a RGBA value array
        uint8_t *data = WebPDecodeRGBA([myData bytes], [myData length], &width, &height);
        
        NSLog(@"Time to decode WebP data: %0.2fms", [startTime timeIntervalSinceNow] * -1000.0);
        
        startTime = [NSDate date];
        
        // Construct a UIImage from the decoded RGBA value array
        provider = CGDataProviderCreateWithData(NULL, data, width*height*4, free_image_data);
    }

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4*width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];
    
    NSLog(@"Time to contruct UIImage from RGBA: %0.2fms", [startTime timeIntervalSinceNow] * -1000.0);
    
    // Set the image into the image view and make image view and the scroll view to the correct size
    self.testImageView.frame = CGRectMake(0, 0, width, height);
    self.testImageView.image = newImage;
    
    self.imageScrollView.contentSize = CGSizeMake(width, height);
    
    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);
}


效果:

111.gif

参考:https://juejin.im/post/6844904089189351438
https://time.geekbang.org/column/article/88573
http://isparta.github.io/
https://github.com/carsonmcdonald/WebP-iOS-example

上一篇 下一篇

猜你喜欢

热点阅读