SDWebImage內(nèi)部實(shí)現(xiàn)過(guò)程(新版本在各方法前加上了sd_前綴,以區(qū)分UIImageView+AFNetworking中的方法)
1.啟動(dòng)入口
setImageWithURL:placeholderImage:options:會(huì)先把 placeholderImage(占位圖片)顯示,然后 SDWebImageManager 根據(jù) URL 開(kāi)始處理圖片。2.進(jìn)入
SDWebImageManager-downloadWithURL:delegate:options:userInfo:,交給 SDImageCache 從緩存查找圖片是否已經(jīng)下載queryDiskCacheForKey:delegate:userInfo:3.先從內(nèi)存圖片緩存查找是否有圖片,如果內(nèi)存中已經(jīng)有圖片緩存,SDImageCacheDelegate 回調(diào)
imageCache:didFindImage:forKey:userInfo:到 SDWebImageManager。4.SDWebImageManagerDelegate 回調(diào)
webImageManager:didFinishWithImage:到 UIImageView+WebCache 等前端展示圖片。5.如果內(nèi)存緩存中沒(méi)有,生成 NSInvocationOperation 添加到隊(duì)列開(kāi)始從硬盤(pán)查找圖片是否已經(jīng)緩存。
6.根據(jù) URLKey 在硬盤(pán)緩存目錄下嘗試讀取圖片文件。這一步是在 NSOperation 進(jìn)行的操作,所以回主線程進(jìn)行結(jié)果回調(diào)
notifyDelegate:。7.如果上一操作從硬盤(pán)讀取到了圖片,將圖片添加到內(nèi)存緩存中(如果空閑內(nèi)存過(guò)小,會(huì)先清空內(nèi)存緩存)。SDImageCacheDelegate 回調(diào)
imageCache:didFindImage:forKey:userInfo:。進(jìn)而回調(diào)展示圖片。8.如果從硬盤(pán)緩存目錄讀取不到圖片,說(shuō)明所有緩存都不存在該圖片,需要下載圖片,回調(diào)
imageCache:didNotFindImageForKey:userInfo:。9.共享或重新生成一個(gè)下載器 SDWebImageDownloader 開(kāi)始下載圖片。
10.圖片下載由 NSURLConnection 來(lái)做,實(shí)現(xiàn)相關(guān) delegate 來(lái)判斷圖片下載中、下載完成和下載失敗。
11.
connection:didReceiveData:中利用 ImageIO 做了按圖片下載進(jìn)度加載效果。12.
connectionDidFinishLoading:數(shù)據(jù)下載完成后交給 SDWebImageDecoder 做圖片解碼處理。13.圖片解碼處理在一個(gè) NSOperationQueue 完成,不會(huì)拖慢主線程 UI。如果有需要對(duì)下載的圖片進(jìn)行二次處理,最好也在這里完成,效率會(huì)好很多。
14.在主線程
notifyDelegateOnMainThreadWithInfo:宣告解碼完成,imageDecoder:didFinishDecodingImage:userInfo:回調(diào)給 SDWebImageDownloader。15.
imageDownloader:didFinishWithImage:回調(diào)給 SDWebImageManager 告知圖片下載完成。16.通知所有的 downloadDelegates 下載完成,回調(diào)給需要的地方展示圖片。
17.將圖片保存到 SDImageCache 中,內(nèi)存緩存和硬盤(pán)緩存同時(shí)保存。寫(xiě)文件到硬盤(pán)也在以單獨(dú) NSInvocationOperation 完成,避免拖慢主線程。
18.SDImageCache 在初始化的時(shí)候會(huì)注冊(cè)一些消息通知,在內(nèi)存警告或退到后臺(tái)的時(shí)候清理內(nèi)存圖片緩存,應(yīng)用結(jié)束的時(shí)候清理過(guò)期圖片。
19.SDWI 也提供了
UIButton+WebCache和MKAnnotationView+WebCache,方便使用。20.SDWebImagePrefetcher 可以預(yù)先下載圖片,方便后續(xù)使用。從上面流程可以看出,當(dāng)你調(diào)用
setImageWithURL:方法的時(shí)候,它會(huì)自動(dòng)去給你干這么多事,當(dāng)你需要在某一具體時(shí)刻做事情的時(shí)候,你可以覆蓋這些方法。比如在下載某個(gè)圖片的過(guò)程中要響應(yīng)一個(gè)事件,就覆蓋這個(gè)方法:
覆蓋方法,指哪打哪,這個(gè)方法是下載imagePath2的時(shí)候響應(yīng)
SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager downloadImageWithURL:imagePath2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"顯示當(dāng)前進(jìn)度"); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { NSLog(@"下載完成"); }];
當(dāng)服務(wù)器更新了某一張圖片資源時(shí),客戶端需要重新加載,那么就可以設(shè)置SDWebImageOption為SDWebImageRefreshCached;附上全部的SDWebImageOptions
typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
/**
* By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying.
* This flag disable this blacklisting.
*/
SDWebImageRetryFailed = 1 << 0,
/**
* By default, image downloads are started during UI interactions, this flags disable this feature,
* leading to delayed download on UIScrollView deceleration for instance.
*/
SDWebImageLowPriority = 1 << 1,
/**
* This flag disables on-disk caching
*/
SDWebImageCacheMemoryOnly = 1 << 2,
/**
* This flag enables progressive download, the image is displayed progressively during download as a browser would do.
* By default, the image is only displayed once completely downloaded.
*/
SDWebImageProgressiveDownload = 1 << 3,
/**
* Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
* The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
* This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
* If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
*
* Use this flag only if you can't make your URLs static with embeded cache busting parameter.
*/
SDWebImageRefreshCached = 1 << 4,
/**
* In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
* extra time in background to let the request finish. If the background task expires the operation will be cancelled.
*/
SDWebImageContinueInBackground = 1 << 5,
/**
* Handles cookies stored in NSHTTPCookieStore by setting
* NSMutableURLRequest.HTTPShouldHandleCookies = YES;
*/
SDWebImageHandleCookies = 1 << 6,
/**
* Enable to allow untrusted SSL ceriticates.
* Useful for testing purposes. Use with caution in production.
*/
SDWebImageAllowInvalidSSLCertificates = 1 << 7,
/**
* By default, image are loaded in the order they were queued. This flag move them to
* the front of the queue and is loaded immediately instead of waiting for the current queue to be loaded (which
* could take a while).
*/
SDWebImageHighPriority = 1 << 8,
/**
* By default, placeholder images are loaded while the image is loading. This flag will delay the loading
* of the placeholder image until after the image has finished loading.
*/
SDWebImageDelayPlaceholder = 1 << 9,
/**
* We usually don't call transformDownloadedImage delegate method on animated images,
* as most transformation code would mangle it.
* Use this flag to transform them anyway.
*/
SDWebImageTransformAnimatedImage = 1 << 10,
};