YYWebImage
?[
[

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
- Update cocoapods to the latest version.
- Add
pod "YYWebImage"to your Podfile. - Run
pod installorpod update. - Import <YYWebImage/YYWebImage.h>
Carthage
- Add
github "ibireme/YYWebImage"to your Cartfile. - Run
carthage update --platform iosand add the framework to your project. - Import <YYWebImage/YYWebImage.h>
- Notice: carthage framework doesn't include webp component, if you want to support webp, use cocoapods or install manually.
Manually
- Download all the files in the YYWebImage subdirectory.
- Add the source files to your Xcode project.
- Link with required frameworks:
- UIKit
- CoreFoundation
- QuartzCore
- AssetsLibrary
- ImageIO
- Accelerate
- MobileCoreServices
- sqlite3
- libz
- Add
Vendor/WebP.framework(static library) to your Xcode project if you want to support WebP. - 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.
中文介紹

YYWebImage 是一個異步圖片加載框架 (YYKit 組件之一).
其設(shè)計(jì)目的是試圖替代 SDWebImage、PINRemoteImage、FLAnimatedImage 等開源框架,它支持這些開源框架的大部分功能,同時增加了大量新特性、并且有不小的性能提升。
它底層用 YYCache 實(shí)現(xiàn)了內(nèi)存和磁盤緩存, 用 YYImage 實(shí)現(xiàn)了 WebP/APNG/GIF 動圖的解碼和播放。
你可以查看這些項(xiàng)目以獲得更多信息。
特性
- 異步的圖片加載,支持 HTTP 和本地文件。
- 支持 GIF、APNG、WebP 動畫(動態(tài)緩存,低內(nèi)存占用)。
- 支持逐行掃描、隔行掃描、漸進(jìn)式圖像加載。
- UIImageView、UIButton、MKAnnotationView、CALayer 的 Category 方法支持。
- 常見圖片處理:模糊、圓角、大小調(diào)整、裁切、旋轉(zhuǎn)、色調(diào)等。
- 高性能的內(nèi)存和磁盤緩存。
- 高性能的圖片設(shè)置方式,以避免主線程阻塞。
- 每個類和方法都有完善的文檔注釋。
用法
從 URL 加載圖片
// 加載網(wǎng)絡(luò)圖片
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"];
漸進(jìn)式圖片加載
// 漸進(jìn)式:邊下載邊顯示
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressive];
// 漸進(jìn)式加載,增加模糊效果和漸變動畫 (見本頁最上方的GIF演示)
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
加載、處理圖片
// 1. 下載圖片
// 2. 獲得圖片下載進(jìn)度
// 3. 調(diào)整圖片大小、加圓角
// 4. 顯示圖片時增加一個淡入動畫,以獲得更好的用戶體驗(yàn)
[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];
// 清空磁盤緩存,帶進(jìn)度回調(diào)
[cache.diskCache removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {
// progress
} endBlock:^(BOOL error) {
// end
}];
安裝
CocoaPods
- 將 cocoapods 更新至最新版本.
- 在 Podfile 中添加
pod "YYWebImage"。 - 執(zhí)行
pod install或pod update。 - 導(dǎo)入 <YYWebImage/YYWebImage.h>。
Carthage
- 在 Cartfile 中添加
github "ibireme/YYWebImage"。 - 執(zhí)行
carthage update --platform ios并將生成的 framework 添加到你的工程。 - 導(dǎo)入 <YYWebImage/YYWebImage.h>。
- 注意: carthage framework 并沒有包含 webp 組件。如果你需要支持 webp,可以用 cocoapods 安裝,或者手動安裝。
手動安裝
- 下載 YYWebImage 文件夾內(nèi)的所有內(nèi)容。
- 將 YYWebImage 內(nèi)的源文件添加(拖放)到你的工程。
- 鏈接以下 frameworks:
- UIKit
- CoreFoundation
- QuartzCore
- AssetsLibrary
- ImageIO
- Accelerate
- MobileCoreServices
- sqlite3
- libz
- 如果你需要支持 WebP,可以將
Vendor/WebP.framework(靜態(tài)庫) 加入你的工程。 - 導(dǎo)入
YYWebImage.h。
文檔
你可以在 CocoaDocs 查看在線 API 文檔,也可以用 appledoc 本地生成文檔。
系統(tǒng)要求
該項(xiàng)目最低支持 iOS 6.0。
許可證
YYWebImage 使用 MIT 許可證,詳情見 LICENSE 文件。