YYWebImage使用方法(轉(zhuǎn)載自官方)

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 是一個(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

  1. 將 cocoapods 更新至最新版本.
  2. 在 Podfile 中添加 pod "YYWebImage"
  3. 執(zhí)行 pod installpod update祸轮。
  4. 導(dǎo)入 <YYWebImage/YYWebImage.h>兽埃。

Carthage

  1. 在 Cartfile 中添加 github "ibireme/YYWebImage"
  2. 執(zhí)行 carthage update --platform ios 并將生成的 framework 添加到你的工程适袜。
  3. 導(dǎo)入 <YYWebImage/YYWebImage.h>柄错。
  4. 注意: carthage framework 并沒有包含 webp 組件。如果你需要支持 webp苦酱,可以用 cocoapods 安裝售貌,或者手動(dòng)安裝。

手動(dòng)安裝

  1. 下載 YYWebImage 文件夾內(nèi)的所有內(nèi)容疫萤。
  2. 將 YYWebImage 內(nèi)的源文件添加(拖放)到你的工程颂跨。
  3. 鏈接以下 frameworks:
    • UIKit
    • CoreFoundation
    • QuartzCore
    • AssetsLibrary
    • ImageIO
    • Accelerate
    • MobileCoreServices
    • sqlite3
    • libz
  4. 如果你需要支持 WebP,可以將 Vendor/WebP.framework(靜態(tài)庫) 加入你的工程给僵。
  5. 導(dǎo)入 YYWebImage.h毫捣。

文檔

你可以在 CocoaDocs 查看在線 API 文檔详拙,也可以用 appledoc 本地生成文檔。

系統(tǒng)要求

該項(xiàng)目最低支持 iOS 6.0蔓同。

許可證

YYWebImage 使用 MIT 許可證饶辙,詳情見 LICENSE 文件。

相關(guān)鏈接

移動(dòng)端圖片格式調(diào)研

iOS 處理圖片的一些小 Tip

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末斑粱,一起剝皮案震驚了整個(gè)濱河市弃揽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌则北,老刑警劉巖矿微,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異尚揣,居然都是意外死亡涌矢,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進(jìn)店門快骗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來娜庇,“玉大人,你說我怎么就攤上這事方篮∶悖” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵藕溅,是天一觀的道長匕得。 經(jīng)常有香客問我,道長巾表,這世上最難降的妖魔是什么汁掠? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮攒发,結(jié)果婚禮上调塌,老公的妹妹穿的比我還像新娘。我一直安慰自己惠猿,他們只是感情好羔砾,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著偶妖,像睡著了一般姜凄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上趾访,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天态秧,我揣著相機(jī)與錄音,去河邊找鬼扼鞋。 笑死申鱼,一個(gè)胖子當(dāng)著我的面吹牛愤诱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播捐友,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼淫半,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了匣砖?” 一聲冷哼從身側(cè)響起科吭,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎猴鲫,沒想到半個(gè)月后对人,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡拂共,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年牺弄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片匣缘。...
    茶點(diǎn)故事閱讀 40,117評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡猖闪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出肌厨,到底是詐尸還是另有隱情,我是刑警寧澤豁陆,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布柑爸,位于F島的核電站,受9級特大地震影響盒音,放射性物質(zhì)發(fā)生泄漏表鳍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一祥诽、第九天 我趴在偏房一處隱蔽的房頂上張望譬圣。 院中可真熱鬧,春花似錦雄坪、人聲如沸厘熟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绳姨。三九已至,卻和暖如春阔挠,著一層夾襖步出監(jiān)牢的瞬間飘庄,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工购撼, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留跪削,地道東北人谴仙。 一個(gè)月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像碾盐,于是被迫代替她去往敵國和親晃跺。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容