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

jopen 9年前發布 | 79K 次閱讀 iOS開發 移動開發 YYWebImage

ProgressiveBlur~

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

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

它底層用 YYCache 實現了內存和磁盤緩存, 用 YYImage 實現了 WebP/APNG/GIF 動圖的解碼和播放。
你可以查看這些項目以獲得更多信息。

特性

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

用法

從 URL 加載圖片

// 加載網絡圖片
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"];

漸進式圖片加載

// 漸進式:邊下載邊顯示
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressive];

// 漸進式加載,增加模糊效果和漸變動畫 (見本頁最上方的GIF演示)
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];

加載、處理圖片

// 1. 下載圖片
// 2. 獲得圖片下載進度
// 3. 調整圖片大小、加圓角
// 4. 顯示圖片時增加一個淡入動畫,以獲得更好的用戶體驗

[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");
        }
    }];

安裝

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:
    • UIKit.framework
    • CoreFoundation.framework
    • QuartzCore.framework
    • AssetsLibrary.framework
    • ImageIO.framework
    • Accelerate.framework
    • MobileCoreServices.framework
    • libsqlite3
    • libz
  4. 如果你需要支持 webp,可以將Vendor/WebP.framework(靜態庫) 加入你的工程。
  5. 導入YYWebImage.h。

項目主頁:http://www.baiduhome.net/lib/view/home/1446474763951

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!