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 是一個異步圖片加載框架 (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

  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 安裝,或者手動安裝。

手動安裝

  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)鏈接

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

iOS 處理圖片的一些小 Tip

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,756評論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,318評論 7 249
  • 張丹 這天晚上 ,我準(zhǔn)時來到學(xué)校門口。隨著一聲放學(xué)鈴響,一波又一波的孩子從學(xué)校涌了出來,女兒的身影卻遲遲未見。 怎...
    z橄欖樹閱讀 233評論 0 0
  • 上周六中午和同學(xué)在人廣附近的廣西北路吃飯的時候,路過一家包子店。聽同學(xué)說,這家店的港式流沙包是上海的人氣美食。當(dāng)時...
    上官若藍(lán)閱讀 487評論 0 0
  • 加拿大時間1月27日,告別了三天的陰天后,天氣特別好,讓我把藍(lán)天和冬天的樹的原貌一睹為快。多倫多的空氣質(zhì)量真好,放...
    美好生活23閱讀 324評論 0 0

友情鏈接更多精彩內(nèi)容