YYWebImage——iOS异步图片加载框架

YYWebImage 是一个异步图片加载框架 (YYKit 组件之一).

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

它底层用 YYCache 实现了内存和磁盘缓存, 用 YYImage 实现了 WebP/APNG/GIF 动图的解码和播放。

你可以查看这些项目以获得更多信息。

特性

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

用法

从 URL 加载图片

1<span style="font-size: medium;">// 加载网络图片
2imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/logo.png"];
3 
4// 加载本地图片
5imageView.yy_imageURL = [NSURL fileURLWithPath:@"/tmp/logo.png"];</span>

加载动图

1<span style="font-size: medium;">// 只需要把 `UIImageView` 替换为 `YYAnimatedImageView` 即可。
2UIImageView *imageView = [YYAnimatedImageView new];
3imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/ani.webp"];</span>

渐进式图片加载

1<span style="font-size: medium;">// 渐进式:边下载边显示
2[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressive];
3 
4// 渐进式加载,增加模糊效果和渐变动画 (见本页最上方的GIF演示)
5[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];</span>

加载、处理图片

1<span style="font-size: medium;">// 1. 下载图片
2// 2. 获得图片下载进度
3// 3. 调整图片大小、加圆角
4// 4. 显示图片时增加一个淡入动画,以获得更好的用户体验
5 
6[imageView yy_setImageWithURL:url
7    placeholder:nil
8    options:YYWebImageOptionSetImageWithFadeAnimation
9    progress:^(NSInteger receivedSize, NSInteger expectedSize) {
10        progress = (float)receivedSize / expectedSize;
11    }
12    transform:^UIImage *(UIImage *image, NSURL *url) {
13        image = [image yy_imageByResizeToSize:CGSizeMake(100, 100) contentMode:UIViewContentModeCenter];
14        return [image yy_imageByRoundCornerRadius:10];
15    }
16    completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {
17        if (from == YYWebImageFromDiskCache) {
18            NSLog(@"load from disk cache");
19        }
20    }];</span>

安装

Cocoapods

  1. 将 cocoapods 更新至最新版本.
  2. 在 Podfile 中添加pod "YYWebImage"。
  3. 执行pod install或pod update。
  4. 导入 <YYWebImage/YYWebImage.h>。

Carthage

  1. 在 Cartfile 中添加github "ibireme/YYWebImage"。
  2. 执行carthage update --platform ios并将生成的 framework 添加到你的工程。
  3. 导入 <YYWebImage/YYWebImage.h>。
  4. 注意: carthage framework 并没有包含 webp 组件。如果你需要支持 webp,可以用 Cocoapods 安装,或者手动安装。

手动安装

  1. 下载 YYWebImage 文件夹内的所有内容。
  2. 将 YYWebModel 内的源文件添加(拖放)到你的工程。
  3. 链接以下 frameworks:如果你需要支持 webp,可以将Vendor/WebP.framework(静态库) 加入你的工程。
    • UIKit.framework
    • CoreFoundation.framework
    • QuartzCore.framework
    • AssetsLibrary.framework
    • ImageIO.framework
    • Accelerate.framework
    • MobileCoreServices.framework
    • libsqlite3
    • libz
  4.  
  5. 导入YYWebImage.h。

 

ibireme / YYWebImage

------------------------------------------------------------------------------------------------------------------------------
 

YYWebImage的基本用法

1.框架集成

* github地址 : https://github.com/ibireme/YYWebImage
* CocoaPods集成 : `pod 'YYWebImage'`

2.准备工作

* 导入主头文件 `#import <YYWebImage.h>`
* 准备控件
    * 展示静态图 `@property (weak, nonatomic) IBOutlet UIImageView *imgView;`
    * 展示GIF/静态图 `@property (weak, nonatomic) IBOutlet YYAnimatedImageView *imgView;`

3.特点

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

4.缓存相关

- (void)cache {

    // 获取缓存管理类
    YYImageCache *cache = [YYWebImageManager sharedManager].cache;

    // 获取内存缓存大小
    NSInteger memCost = cache.memoryCache.totalCost/1024;
    // 获取内存缓存个数
    NSInteger memCount = cache.memoryCache.totalCount;
    // 获取磁盘缓存大小
    NSInteger diskCost = cache.diskCache.totalCost/1024;
    // 获取磁盘缓存个数
    NSInteger diskCount = cache.diskCache.totalCount;

    // 清空缓存内存缓存
    [cache.memoryCache removeAllObjects];
    // 清空磁盘缓存
    [cache.diskCache removeAllObjects];
    
    // 清空磁盘缓存,带进度回调
    [cache.diskCache removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {
    // progress
    } endBlock:^(BOOL error) {
    // end
    }];
}


5.加载、处理图片

  1. 下载图片
  2. 获得图片下载进度
  3. 调整图片大小
  4. 显示图片时增加一个淡入动画,以获得更好的用户体验
- (void)loadImageAndHandle {

    NSURL *URL = [NSURL URLWithString:@"http://img.taopic.com/uploads/allimg/130815/235089-130Q513004496.jpg"];

    [self.imgView yy_setImageWithURL:URL placeholder:nil options:YYWebImageOptionSetImageWithFadeAnimation progress:^(NSInteger receivedSize, NSInteger expectedSize) {

        // 计算进度
        float progress = (float)receivedSize/expectedSize;
        NSLog(@"%f",progress);

    } transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {

        // 可以额外处理图片在返回 : 截取中心点周围100x100的范围
//        return [image yy_imageByResizeToSize:CGSizeMake(100, 100) contentMode:UIViewContentModeCenter];

        // 返回原始图片
        return image;

    } completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {

        // 测试 : 是否是从磁盘加载的
        if (from == YYWebImageFromDiskCache) {
            NSLog(@"load from disk cache");
        }
    }];
}

6.渐进式下载

  • 控制options参数即可
- (void)loadProgressive {

    NSURL *URL;

    URL = [NSURL URLWithString:@"http://img.taopic.com/uploads/allimg/110721/8114-110H109430691.jpg"];
    // 渐进式 : 边下载边显示
//    [self.imgView yy_setImageWithURL:URL options:YYWebImageOptionProgressive];

    URL = [NSURL URLWithString:@"http://pic11.nipic.com/20101125/5592256_172526068815_2.jpg"];
    // 渐进式 : 增加模糊效果和渐变动画
    [self.imgView yy_setImageWithURL:URL options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
}

7.加载动图

  • 展示动图的自定义控件 YYAnimatedImageView
- (void)loadGIF {

    NSURL *URL = [NSURL URLWithString:@"http://image.nihaowang.com/news/2015-04-27/c30f866d-9300-4f6e-86f6-58f408630e14.gif"];

    self.imgView.yy_imageURL = URL;
}

8.普通用法,最常用的

@property (weak, nonatomic) YYAnimatedImageView *imageView;

- (void)loadImageNormal {

    NSURL *URL = [NSURL URLWithString:@"http://img01.taopic.com/150509/318761-15050ZP24418.jpg"];

    // 可以设置占位图
    [self.imgView yy_setImageWithURL:URL placeholder:nil];
    // 不可以设置占位图
//    self.imgView.yy_imageURL = URL;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值