
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" ]; |
5 |
imageView.yy_imageURL = [NSURL fileURLWithPath:@ "/tmp/logo.png" ];</span> |
加載動圖
1 |
<span style= "font-size: medium;" > |
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]; |
5 |
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];</span> |
加載、處理圖片
1 |
<span style= "font-size: medium;" > |
6 |
[imageView yy_setImageWithURL:url |
8 |
options:YYWebImageOptionSetImageWithFadeAnimation |
9 |
progress:^(NSInteger receivedSize, NSInteger expectedSize) { |
10 |
progress = ( float )receivedSize / expectedSize; |
12 |
transform:^UIImage *(UIImage *image, NSURL *url) { |
13 |
image = [image yy_imageByResizeToSize:CGSizeMake(100, 100) contentMode:UIViewContentModeCenter]; |
14 |
return [image yy_imageByRoundCornerRadius:10]; |
16 |
completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) { |
17 |
if (from == YYWebImageFromDiskCache) { |
18 |
NSLog(@ "load from disk cache" ); |
安裝
Cocoapods
- 將 cocoapods 更新至最新版本.
- 在 Podfile 中添加pod "YYWebImage"。
- 執行pod install或pod update。
- 導入 <YYWebImage/YYWebImage.h>。
Carthage
- 在 Cartfile 中添加github "ibireme/YYWebImage"。
- 執行carthage update --platform ios並將生成的 framework 添加到你的工程。
- 導入 <YYWebImage/YYWebImage.h>。
- 注意: carthage framework 並沒有包含 webp 組件。如果你需要支持 webp,可以用 Cocoapods 安裝,或者手動安裝。
手動安裝
- 下載 YYWebImage 文件夾內的所有內容。
- 將 YYWebModel 內的源文件添加(拖放)到你的工程。
- 鏈接以下 frameworks:如果你需要支持 webp,可以將Vendor/WebP.framework(靜態庫) 加入你的工程。
- UIKit.framework
- CoreFoundation.framework
- QuartzCore.framework
- AssetsLibrary.framework
- ImageIO.framework
- Accelerate.framework
- MobileCoreServices.framework
- libsqlite3
- libz
- 導入YYWebImage.h。
項目描述:Asynchronous image loading framework. — 查看更多內容...