YYWebImage——iOS異步圖片加載框架


本文轉載至 http://www.mobile-open.com/2015/86582.html

 

一個異步圖片加載框架:YYWebImage

YYWebImage 是一個異步圖片加載框架 (YYKit 組件之一).

其設計目的是試圖替代 SDWebImage、PINRemoteImage、FLAnimatedImage 等開源框架,它支持這些開源框架的大部分功能,同時增加了大量新特性、並且有不小的性能提升。

它底層用 YYCache 實現了內存和磁盤緩存, 用 YYImage 實現了 WebP/APNG/GIF 動圖的解碼和播放。

你可以查看這些項目以獲得更多信息。

特性

  • 異步的圖片加載,支持 HTTP 和本地文件。
  • 支持 WebP、APNG、GIF 動畫。
  • 支持逐行掃描、隔行掃描、漸進式圖像加載。
  • UIImageView、UIButton、MKAnnotationView、CALayer 的 Category 方法支持。
  • 常見圖片處理:模糊、圓角、大小調整、裁切、旋轉、色調等。
  • 高性能的內存和磁盤緩存。
  • 高性能的圖片設置方式,以避免主線程阻塞。
  • 每個類和方法都有完善的文檔注釋。

用法

從 URL 加載圖片

1 <span style="font-size: medium;">// 加載網絡圖片
2 imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/logo.png"];
3  
4 // 加載本地圖片
5 imageView.yy_imageURL = [NSURL fileURLWithPath:@"/tmp/logo.png"];</span>

加載動圖

1 <span style="font-size: medium;">// 只需要把 `UIImageView` 替換為 `YYAnimatedImageView` 即可。
2 UIImageView *imageView = [YYAnimatedImageView new];
3 imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/ani.webp"];</span>

漸進式圖片加載

1 <span style="font-size: medium;">// 漸進式:邊下載邊顯示
2 [imageView yy_setImageWithURL:url options:YYWebImageOptionProgressive];
3  
4 // 漸進式加載,增加模糊效果和漸變動畫 (見本頁最上方的GIF演示)
5 [imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];</span>

加載、處理圖片

1 <span style="font-size: medium;">// 1. 下載圖片
2 // 2. 獲得圖片下載進度
3 // 3. 調整圖片大小、加圓角
4 // 4. 顯示圖片時增加一個淡入動畫,以獲得更好的用戶體驗
5  
6 [imageView yy_setImageWithURL:url
7     placeholder:nil
8     options:YYWebImageOptionSetImageWithFadeAnimation
9     progress:^(NSInteger receivedSize, NSInteger expectedSize) {
10         progress = (float)receivedSize / expectedSize;
11     }
12     transform:^UIImage *(UIImage *image, NSURL *url) {
13         image = [image yy_imageByResizeToSize:CGSizeMake(100, 100) contentMode:UIViewContentModeCenter];
14         return [image yy_imageByRoundCornerRadius:10];
15     }
16     completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {
17         if (from == YYWebImageFromDiskCache) {
18             NSLog(@"load from disk cache");
19         }
20     }];</span>

安裝

Cocoapods

  1. 將 cocoapods 更新至最新版本.
  2. 在 Podfile 中添加pod "YYWebImage"。
  3. 執行pod install或pod 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. 將 YYWebModel 內的源文件添加(拖放)到你的工程。
  3. 鏈接以下 frameworks:如果你需要支持 webp,可以將Vendor/WebP.framework(靜態庫) 加入你的工程。
    • UIKit.framework
    • CoreFoundation.framework
    • QuartzCore.framework
    • AssetsLibrary.framework
    • ImageIO.framework
    • Accelerate.framework
    • MobileCoreServices.framework
    • libsqlite3
    • libz
  4. 導入YYWebImage.h。

 

ibireme / YYWebImage

Watch 1200 Fork 156

項目描述:Asynchronous image loading framework. — 查看更多內容...


 

分支代碼更新時間:2015-11-11

開發語言:Objective-C

ZIP文件下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM