YYWebImage使用方法(转载自官方)

YYWebImage

 
Carthage compatible
Carthage compatible
 
[
CocoaPods
CocoaPods
](http://cocoapods.org/?q= YYWebImage) 
[
CocoaPods
CocoaPods
](http://cocoapods.org/?q= YYWebImage) 
Support
Support
 
Build Status
Build Status

ProgressiveBlur~
ProgressiveBlur~

YYWebImage is an asynchronous image loading framework (a component of YYKit).

It was created as an improved replacement for SDWebImage, PINRemoteImage and FLAnimatedImage.

It use YYCache to support memory and disk cache, and YYImage to support WebP/APNG/GIF image decode.

See these project for more information.

Features

  • Asynchronous image load from remote or local URL.
  • Animated WebP, APNG, GIF support (dynamic buffer, lower memory usage).
  • Baseline/progressive/interlaced image decode support.
  • Image loading category for UIImageView, UIButton, MKAnnotationView and CALayer.
  • Image effect: blur, round corner, resize, color tint, crop, rotate and more.
  • High performance memory and disk image cache.
  • High performance image loader to avoid main thread blocked.
  • Fully documented.

Usage

Load image from URL

// load from remote url
imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/logo.png"];

// load from local url
imageView.yy_imageURL = [NSURL fileURLWithPath:@"/tmp/logo.png"];

Load animated image

// just replace `UIImageView` with `YYAnimatedImageView`
UIImageView *imageView = [YYAnimatedImageView new];
imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/ani.webp"];

Load image progressively

// progressive
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressive];

// progressive with blur and fade animation (see the demo at the top of this page)
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];

Load and process image

// 1. download image from remote
// 2. get download progress
// 3. resize image and add round corner
// 4. set image with a fade animation

[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");
        }
    }];

Image Cache

YYImageCache *cache = [YYWebImageManager sharedManager].cache;

// get cache capacity
cache.memoryCache.totalCost;
cache.memoryCache.totalCount;
cache.diskCache.totalCost;
cache.diskCache.totalCount;

// clear cache
[cache.memoryCache removeAllObjects];
[cache.diskCache removeAllObjects];

// clear disk cache with progress
[cache.diskCache removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {
    // progress
} endBlock:^(BOOL error) {
    // end
}];

Installation

CocoaPods

  1. Update cocoapods to the latest version.
  2. Add pod "YYWebImage" to your Podfile.
  3. Run pod install or pod update.
  4. Import <YYWebImage/YYWebImage.h>

Carthage

  1. Add github "ibireme/YYWebImage" to your Cartfile.
  2. Run carthage update --platform ios and add the framework to your project.
  3. Import <YYWebImage/YYWebImage.h>
  4. Notice: carthage framework doesn't include webp component, if you want to support webp, use cocoapods or install manually.

Manually

  1. Download all the files in the YYWebImage subdirectory.
  2. Add the source files to your Xcode project.
  3. Link with required frameworks:
    • UIKit
    • CoreFoundation
    • QuartzCore
    • AssetsLibrary
    • ImageIO
    • Accelerate
    • MobileCoreServices
    • sqlite3
    • libz
  4. Add Vendor/WebP.framework(static library) to your Xcode project if you want to support WebP.
  5. Import YYWebImage.h.

Documentation

Full API documentation is available on CocoaDocs.

You can also install documentation locally using appledoc.

Requirements

This library requires a deployment target of iOS 6.0 or greater.

License

YYWebImage is provided under the MIT license. See LICENSE file for details.



中文介绍

ProgressiveBlur~
ProgressiveBlur~

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

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

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

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

特性

  • 异步的图片加载,支持 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
}];

安装

CocoaPods

  1. 将 cocoapods 更新至最新版本.
  2. 在 Podfile 中添加 pod "YYWebImage"
  3. 执行 pod installpod 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. 将 YYWebImage 内的源文件添加(拖放)到你的工程。
  3. 链接以下 frameworks:
    • UIKit
    • CoreFoundation
    • QuartzCore
    • AssetsLibrary
    • ImageIO
    • Accelerate
    • MobileCoreServices
    • sqlite3
    • libz
  4. 如果你需要支持 WebP,可以将 Vendor/WebP.framework(静态库) 加入你的工程。
  5. 导入 YYWebImage.h

文档

你可以在 CocoaDocs 查看在线 API 文档,也可以用 appledoc 本地生成文档。

系统要求

该项目最低支持 iOS 6.0。

许可证

YYWebImage 使用 MIT 许可证,详情见 LICENSE 文件。

相关链接

移动端图片格式调研

iOS 处理图片的一些小 Tip

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容