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 install
orpod update
. - Import <YYWebImage/YYWebImage.h>
Carthage
- Add
github "ibireme/YYWebImage"
to your Cartfile. - Run
carthage update --platform ios
and 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 是一個(gè)異步圖片加載框架 (YYKit 組件之一).
其設(shè)計(jì)目的是試圖替代 SDWebImage昆庇、PINRemoteImage名扛、FLAnimatedImage 等開源框架助析,它支持這些開源框架的大部分功能综芥,同時(shí)增加了大量新特性、并且有不小的性能提升辩块。
它底層用 YYCache 實(shí)現(xiàn)了內(nèi)存和磁盤緩存, 用 YYImage 實(shí)現(xiàn)了 WebP/APNG/GIF 動(dòng)圖的解碼和播放烙常。
你可以查看這些項(xiàng)目以獲得更多信息。
特性
- 異步的圖片加載召耘,支持 HTTP 和本地文件百炬。
- 支持 GIF、APNG污它、WebP 動(dòng)畫(動(dòng)態(tài)緩存剖踊,低內(nèi)存占用)庶弃。
- 支持逐行掃描、隔行掃描德澈、漸進(jìn)式圖像加載歇攻。
- UIImageView、UIButton梆造、MKAnnotationView缴守、CALayer 的 Category 方法支持。
- 常見圖片處理:模糊澳窑、圓角斧散、大小調(diào)整、裁切摊聋、旋轉(zhuǎn)、色調(diào)等栈暇。
- 高性能的內(nèi)存和磁盤緩存麻裁。
- 高性能的圖片設(shè)置方式,以避免主線程阻塞源祈。
- 每個(gè)類和方法都有完善的文檔注釋煎源。
用法
從 URL 加載圖片
// 加載網(wǎng)絡(luò)圖片
imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/logo.png"];
// 加載本地圖片
imageView.yy_imageURL = [NSURL fileURLWithPath:@"/tmp/logo.png"];
加載動(dòng)圖
// 只需要把 `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)式加載香缺,增加模糊效果和漸變動(dòng)畫 (見本頁最上方的GIF演示)
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
加載手销、處理圖片
// 1. 下載圖片
// 2. 獲得圖片下載進(jìn)度
// 3. 調(diào)整圖片大小、加圓角
// 4. 顯示圖片時(shí)增加一個(gè)淡入動(dòng)畫图张,以獲得更好的用戶體驗(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 安裝售貌,或者手動(dòng)安裝。
手動(dòng)安裝
- 下載 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 文件。