YYWebImage使用

来源:互联网 发布:linux解压tar包 编辑:程序博客网 时间:2024/06/05 22:58

YYWebImage 是一个异步图片加载框架 (YYKit 组件之一), 性能相对较高,下面来简单说一下。
**

特性

**:

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

安装:

个人推荐使用cocoapods安装

  1. 在 Podfile 中添加 pod ‘YYWebImage’。
  2. 执行 pod install 或 pod update。
  3. 导入 YYWebImage.h

常见使用方法:

从 URL 加载图片:

// 加载网络图片imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/logo.png"];// 加载本地图片imageView.yy_imageURL = [NSURL fileURLWithPath:@"/tmp/logo.png"];

加载动图:

// 只需要把 `UIImageView` 替换为 `YYAnimatedImageView` 即可。UIImageView *imageView = [YYAnimatedImageView new];imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/ani.webp"];

渐进式图片加载:

// 渐进式:边下载边显示[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressive];// 渐进式加载,增加模糊效果和渐变动画 (见本页最上方的GIF演示)[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];

加载、处理图片:

// 1. 下载图片// 2. 获得图片下载进度// 3. 调整图片大小、加圆角// 4. 显示图片时增加一个淡入动画,以获得更好的用户体验[imageView yy_setImageWithURL:url   placeholder:nil   options:YYWebImageOptionSetImageWithFadeAnimation   progress:^(NSInteger receivedSize, NSInteger expectedSize) {       progress = (float)receivedSize / expectedSize;   }   transform:^UIImage *(UIImage *image, NSURL *url) {       image = [image yy_imageByResizeToSize:CGSizeMake(100, 100) contentMode:UIViewContentModeCenter];       return [image yy_imageByRoundCornerRadius:10];   }   completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {       if (from == YYWebImageFromDiskCache) {           NSLog(@"load from disk cache");       }   }];

图片缓存:

YYImageCache *cache = [YYWebImageManager sharedManager].cache;// 获取缓存大小cache.memoryCache.totalCost;cache.memoryCache.totalCount;cache.diskCache.totalCost;cache.diskCache.totalCount;// 清空缓存[cache.memoryCache removeAllObjects];[cache.diskCache removeAllObjects];// 清空磁盘缓存,带进度回调[cache.diskCache removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {   // progress} endBlock:^(BOOL error) {   // end}];

以上就是YYWebImage的常用用法,希望大家可以借鉴!

原创粉丝点击