YYWebImage的基本使用: 与SDWebImage 相比在性能方面有很大的提升,YYWebImage 的图片加载的时候不是直接加载原图,而是通过图片重绘的形式将图片加载上去的,性能消耗很小

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

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

它底层用 YYCache 实现了内存和磁盘缓存, 用 YYImage 实现了 WebP/APNG/GIF 动图的解码和播放。<br/>
你可以查看这些项目以获得更多信息。

特性

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

用法

从 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
}];
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值