SDWebImage
https://github.com/rs/SDWebImage
提供一個UIImageView 類別以支持加載來自網絡的遠程圖片。
具有緩存管理,異步下載,同一個URL下載次數控制和優化等
UITableView UIImageView +webCache 類
UIImageView +WebCache.h tableView cellForRowAtIndexPath
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cel = [tableView dequeReuseableCellWithIdentifier.MyIdentifier]
if(cell == nil) {
cell =
}
[cell.imageView setImageWithURL:[NSURL URLWithString:@"htt://www.domain/path/to /image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"];
cell.textLabel.text = @"MyText";
return cell;
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cacheImage = [manager imageWithURL:url];
if(cachedImage) {
} else {
[manager downloadWithURL:url delegate:self];
}
protocol webImageManager:didFinishWithImage方法
downloader= [SDWebImageDownloader downloaderWithURL:url delegate:self]
獨立的異步圖片緩存
SDWebImage支持異步的圖片下載+緩存
1 入口setImageWithURL:placeholderImage:option 先把placeholderImage顯示后,
然后SDWebImageManage根據URL開始處理圖片
進入SDWebImageManager-downloadWithURL:delegate:options:userinfo
交給SDImageCache從緩存查找圖片是否已下載
queryDiskCacheForKey:delegate:userinfo
先從內存圖片查找是否有圖片, 如果內存中已經有圖片存,
imageCach:didFindImage:forKey:userInfo
如果內存中沒有,生成NSInvocationOperation 到開始從硬盤查找圖片圖片是否已 存
根據URLKey 在硬盤存目錄下嘗試讀取圖片,這是NSOperation操作,
notifyDelegate
imageCache:didNotFindImageForKey:userinfo
共享或重新生成一個下載器SDWebImageDownloader開始下載圖片。
下載由NSURLConnection 來做,實現相關delegate 來判斷圖片下載中。
connection:didReceiveData:中利用IMageIO做了按圖片下載進度加載效果。
connection:didFinishLoading 數據下載完成后交給SDWebImageDecoder做
圖片解碼處理
NSOperationQueue完成,不會拖慢主線程UI,
notifyDelegateOnMainThreadWithInfo宣告解碼完成,imageDecoder:didFinishDecodingImage:userInfo
image
保存到SDImageCache中, 內存存和硬盤緩存同時保存。
NSInvocationOperation完成,