SDWebImage內部實現(xiàn)過程(新版本在各方法前加上了sd_前綴缓熟,以區(qū)分UIImageView+AFNetworking中的方法)
1.啟動入口
setImageWithURL:placeholderImage:options:
會先把 placeholderImage(占位圖片)顯示,然后 SDWebImageManager 根據(jù) URL 開始處理圖片侯谁。2.進入
SDWebImageManager-downloadWithURL:delegate:options:userInfo:
消略,交給 SDImageCache 從緩存查找圖片是否已經(jīng)下載queryDiskCacheForKey:delegate:userInfo:
3.先從內存圖片緩存查找是否有圖片堡称,如果內存中已經(jīng)有圖片緩存,SDImageCacheDelegate 回調
imageCache:didFindImage:forKey:userInfo:
到 SDWebImageManager艺演。4.SDWebImageManagerDelegate 回調
webImageManager:didFinishWithImage:
到 UIImageView+WebCache 等前端展示圖片却紧。5.如果內存緩存中沒有,生成 NSInvocationOperation 添加到隊列開始從硬盤查找圖片是否已經(jīng)緩存胎撤。
6.根據(jù) URLKey 在硬盤緩存目錄下嘗試讀取圖片文件晓殊。這一步是在 NSOperation 進行的操作,所以回主線程進行結果回調
notifyDelegate:
伤提。7.如果上一操作從硬盤讀取到了圖片巫俺,將圖片添加到內存緩存中(如果空閑內存過小,會先清空內存緩存)肿男。SDImageCacheDelegate 回調
imageCache:didFindImage:forKey:userInfo:
介汹。進而回調展示圖片。8.如果從硬盤緩存目錄讀取不到圖片舶沛,說明所有緩存都不存在該圖片嘹承,需要下載圖片,回調
imageCache:didNotFindImageForKey:userInfo:
如庭。9.共享或重新生成一個下載器 SDWebImageDownloader 開始下載圖片叹卷。
10.圖片下載由 NSURLConnection 來做,實現(xiàn)相關 delegate 來判斷圖片下載中、下載完成和下載失敗豪娜。
11.
connection:didReceiveData:
中利用 ImageIO 做了按圖片下載進度加載效果餐胀。12.
connectionDidFinishLoading:
數(shù)據(jù)下載完成后交給 SDWebImageDecoder 做圖片解碼處理哟楷。13.圖片解碼處理在一個 NSOperationQueue 完成瘤载,不會拖慢主線程 UI。如果有需要對下載的圖片進行二次處理卖擅,最好也在這里完成鸣奔,效率會好很多。
14.在主線程
notifyDelegateOnMainThreadWithInfo:
宣告解碼完成惩阶,imageDecoder:didFinishDecodingImage:userInfo:
回調給 SDWebImageDownloader挎狸。15.
imageDownloader:didFinishWithImage:
回調給 SDWebImageManager 告知圖片下載完成。16.通知所有的 downloadDelegates 下載完成断楷,回調給需要的地方展示圖片锨匆。
17.將圖片保存到 SDImageCache 中,內存緩存和硬盤緩存同時保存冬筒。寫文件到硬盤也在以單獨 NSInvocationOperation 完成恐锣,避免拖慢主線程。
18.SDImageCache 在初始化的時候會注冊一些消息通知舞痰,在內存警告或退到后臺的時候清理內存圖片緩存土榴,應用結束的時候清理過期圖片。
19.SDWI 也提供了
UIButton+WebCache
和MKAnnotationView+WebCache
响牛,方便使用玷禽。20.SDWebImagePrefetcher 可以預先下載圖片,方便后續(xù)使用呀打。從上面流程可以看出矢赁,當你調用
setImageWithURL:
方法的時候,它會自動去給你干這么多事贬丛,當你需要在某一具體時刻做事情的時候撩银,你可以覆蓋這些方法。比如在下載某個圖片的過程中要響應一個事件瘫寝,就覆蓋這個方法:
覆蓋方法蜒蕾,指哪打哪,這個方法是下載imagePath2的時候響應
SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager downloadImageWithURL:imagePath2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"顯示當前進度"); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { NSLog(@"下載完成"); }];
當服務器更新了某一張圖片資源時焕阿,客戶端需要重新加載咪啡,那么就可以設置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,
};