source https://github.com/rs/SDWebImage
APIdoc http://hackemist.com/SDWebImage/doc
Asynchronous image downloader with cache support with an UIImageView category
UIImageView的類目,支持異步圖片下載,支持緩存機制
This library provides a category for UIImageVIew with support for remote images coming from the web.
這個庫給UIImageView提供類目,支持遠程下載圖片(從網絡上)
It provides:
- An UIImageView category adding web image and cache management to the Cocoa Touch framework
- An asynchronous image downloader
- An asynchronous memory + disk image caching with automatic cache expiration handling
- Animated GIF support
- WebP format support
- A background image decompression
- A guarantee that the same URL won't be downloaded several times
- A guarantee that bogus URLs won't be retried again and again
- A guarantee that main thread will never be blocked
- Performances!
- Use GCD and ARC
- Arm64 support
- 一個UIImageView的類目,給 Cocoa Touch 框架添加了異步下載遠程圖片以及管理圖片緩存的功能
- 一個圖片的異步下載器
- 一個內存 + 磁盤的緩存機制,並自動管理
- gif動畫支持
- WebP格式支持
- 后台解壓圖片
- 確保同樣地 URL 不會重復的下載多次
- 確保無效的 URL 不會重復的鏈接
- 確保主線程永遠不會阻塞
- 效果拔群!
- 使用GCD以及要求ARC
- 支持64位系統
以下進行SDWebImage使用的教程解說.
1. 從地址 https://github.com/rs/SDWebImage 下載源碼,將源碼包中得 SDWebImage 文件夾拖入你的工程當中.
2. 頭文件較多,請新建一個 SDWebImage.h 的頭文件,寫以下代碼並包含所有頭文件,添加到.pch文件中
-------------------------------------------------------------------------------
//MKAnnotationView地圖的注解View緩存
#import "MKAnnotationView+WebCache.h"
//判斷NSData是否什么類型的圖片(例如:jpg,png,gif)
#import "NSData+ImageContentType.h"
//是SDWebImage包的一部分
#import "SDImageCache.h" //緩存相關
#import "SDWebImageCompat.h" //組件相關
#import "SDWebImageDecoder.h" //解碼相關
//圖片下載以及下載管理器
#import "SDWebImageDownloader.h"
#import "SDWebImageDownloaderOperation.h"
//管理以及操作
#import "SDWebImageManager.h"
#import "SDWebImageOperation.h"
//UIButton類目
#import "UIButton+WebCache.h"
//gif類目
#import "UIImage+GIF.h"
//圖片其他類目
#import "UIImage+MultiFormat.h"
#import "UIImage+WebP.h"
#import "UIImageView+WebCache.h"
-------------------------------------------------------------------------------
3. 正式開始講解怎么使用
獨立的下載圖片的功能(沒有緩存機制)
NSString *oneImageURL = @"http://wallpapers.wallbase.cc/rozne/wallpaper-573934.jpg"; [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:oneImageURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { //此處為下載進度 } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { //下載完成后進入這里執行 }];
分析:此方法為單例模式,看其源碼
+ (SDWebImageDownloader *)sharedDownloader {
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}
- (id)init {
if ((self = [super init])) {
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
_downloadQueue = [NSOperationQueue new];
_downloadQueue.maxConcurrentOperationCount = 2;
_URLCallbacks = [NSMutableDictionary new];
_HTTPHeaders = [NSMutableDictionary dictionaryWithObject:@"image/webp,image/*;q=0.8" forKey:@"Accept"];
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
_downloadTimeout = 15.0;
}
return self;
}
typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
/**
* Default value. All download operations will execute in queue style (first-in-first-out). 默認值.所有的下載操作將會進入串行線程池FIFO
*/
SDWebImageDownloaderFIFOExecutionOrder,
/**
* All download operations will execute in stack style (last-in-first-out).
*/
SDWebImageDownloaderLIFOExecutionOrder
};
如果僅僅看上面的部分,知道下載單例由串行線程池管理着,按照隊列執行,一次最多能執行兩個,但我在實際測試過程中發現,並不像描述的那樣子......,好吧,就當做是並發執行的了(此處疑問有時間再解決)
獨立的下載圖片的功能(有緩存機制)
NSString *oneImageURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDWebImageManager sharedManager] downloadWithURL:[NSURL URLWithString:oneImageURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { //此處為下載進度 } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { //下載完成后進入這里執行 }];
清除緩存文件
[[SDImageCache sharedImageCache] clearDisk];
判斷本地緩存中是否存在網絡中的圖片
NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDImageCache sharedImageCache] diskImageExistsWithKey:imageNetURL];
獲取緩存圖片張數
[[SDImageCache sharedImageCache] getDiskCount];
獲取所有緩存圖片的總大小
[[SDImageCache sharedImageCache] getSize];
直接從緩存中提取圖片
NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageNetURL];
直接刪除緩存中得圖片
NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDImageCache sharedImageCache] removeImageForKey:imageNetURL];
在UITableView中使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"Y.X."; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; } // Here we use the new provided setImageWithURL: method to load the web image [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; cell.textLabel.text = @"Y.X."; return cell; }
-未完待續-