关于ios项目中加入webp格式的图片

webp格式图片在iOS中的使用

主要讨论iOS客户端如何支持webp格式的图片链接显示。webp的相关知识点可百度。

使用框架:YYWebImage

YYWebImage 是一个异步图片加载框架 

其设计目的是试图替代 SDWebImage、PINRemoteImage、FLAnimatedImage 等开源框架,它支持这些开源框架的大部分功能,同时增加了大量新特性、并且有不小的性能提升。

特性

  • 异步的图片加载,支持 HTTP 和本地文件。
  • 支持 GIF、APNG、WebP 动画(动态缓存,低内存占用)。
  • 支持逐行扫描、隔行扫描、渐进式图像加载。
  • UIImageView、UIButton、MKAnnotationView、CALayer 的 Category 方法支持。
  • 常见图片处理:模糊、圆角、大小调整、裁切、旋转、色调等。
  • 高性能的内存和磁盘缓存。
  • 高性能的图片设置方式,以避免主线程阻塞。
  • 每个类和方法都有完善的文档注释。

用法

  1. 在 Podfile 中添加 pod 'YYImage/WebP'
  2. 执行 pod install 或 pod update。
  3. 导入 <YYWebImage/YYWebImage.h>。

代码

webp图片链接的简单使用方式,直接通过url加载。

    UIImageView *imageView = [[UIImageView alloc]init];
    NSString *urlStr = @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp";
    imageView.yy_imageURL = [NSURL URLWithString:urlStr];

检测下载进度和图片完成后transform处理

    UIImageView *imageView = [[UIImageView alloc]init];
    NSString *urlStr = @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp";
    [self.view addSubview:imageView];
    imageView.frame = CGRectMake(0, 0, 100, 100);
    [imageView yy_setImageWithURL:[NSURL URLWithString:urlStr]
                      placeholder:nil
                          options:YYWebImageOptionSetImageWithFadeAnimation
                         progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                             NSLog(@"%f",(float)receivedSize / expectedSize);
                         }
                        transform:^UIImage *(UIImage *image, NSURL *url) {
//                            image = [image yy_imageByResizeToSize:CGSizeMake(100, 100) contentMode:UIViewContentModeCenter];
//                            return [image yy_imageByRoundCornerRadius:10];
                            return image;
                        }
                       completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {
                           if (from == YYWebImageFromDiskCache) {
                               NSLog(@"load from disk cache");
                           }
                       }];

 

posted @ 2017-11-24 14:33  Nuius  阅读(1411)  评论(0编辑  收藏  举报