IOS開發(fā)筆記之淺讀SDWebImage實現(xiàn)原理

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+WebCacheMKAnnotationView+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,
};
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末暮屡,一起剝皮案震驚了整個濱河市撤摸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖准夷,帶你破解...
    沈念sama閱讀 211,743評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件钥飞,死亡現(xiàn)場離奇詭異,居然都是意外死亡衫嵌,警方通過查閱死者的電腦和手機读宙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來楔绞,“玉大人结闸,你說我怎么就攤上這事【贫洌” “怎么了桦锄?”我有些...
    開封第一講書人閱讀 157,285評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蔫耽。 經(jīng)常有香客問我结耀,道長,這世上最難降的妖魔是什么匙铡? 我笑而不...
    開封第一講書人閱讀 56,485評論 1 283
  • 正文 為了忘掉前任图甜,我火速辦了婚禮,結果婚禮上慰枕,老公的妹妹穿的比我還像新娘具则。我一直安慰自己,他們只是感情好具帮,可當我...
    茶點故事閱讀 65,581評論 6 386
  • 文/花漫 我一把揭開白布博肋。 她就那樣靜靜地躺著,像睡著了一般蜂厅。 火紅的嫁衣襯著肌膚如雪匪凡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,821評論 1 290
  • 那天掘猿,我揣著相機與錄音病游,去河邊找鬼。 笑死稠通,一個胖子當著我的面吹牛衬衬,可吹牛的內容都是我干的。 我是一名探鬼主播改橘,決...
    沈念sama閱讀 38,960評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼滋尉,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了飞主?” 一聲冷哼從身側響起狮惜,我...
    開封第一講書人閱讀 37,719評論 0 266
  • 序言:老撾萬榮一對情侶失蹤高诺,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后碾篡,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體虱而,經(jīng)...
    沈念sama閱讀 44,186評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,516評論 2 327
  • 正文 我和宋清朗相戀三年开泽,在試婚紗的時候發(fā)現(xiàn)自己被綠了牡拇。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,650評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡眼姐,死狀恐怖诅迷,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情众旗,我是刑警寧澤,帶...
    沈念sama閱讀 34,329評論 4 330
  • 正文 年R本政府宣布趟畏,位于F島的核電站贡歧,受9級特大地震影響,放射性物質發(fā)生泄漏赋秀。R本人自食惡果不足惜利朵,卻給世界環(huán)境...
    茶點故事閱讀 39,936評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望猎莲。 院中可真熱鬧绍弟,春花似錦、人聲如沸著洼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,757評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽身笤。三九已至豹悬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間液荸,已是汗流浹背瞻佛。 一陣腳步聲響...
    開封第一講書人閱讀 31,991評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留娇钱,地道東北人伤柄。 一個月前我還...
    沈念sama閱讀 46,370評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像文搂,于是被迫代替她去往敵國和親适刀。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,527評論 2 349

推薦閱讀更多精彩內容