SDWebImage 源碼一覽

SDImageCache

SDMemoryCache (只針對 iOS, 其餘系統與 NSCache 同樣)

SDMemoryCache: 繼承於NSCache. 聲明三個私有屬性: config, weakCache, weakCacheLockgit

  • config: SDImageCacheConfig 緩存配置信息
  • weakCache: NSMapTable<KeyType, ObjectType> 緩存信息
  • weakCacheLock: dispatch_semaphore_t 鎖,保證數據安全
/// 初始化 config, weakCache, weakCacheLock 變量, 添加一個內存警告監聽, 內存警告時釋放內存.
 - (instancetype)initWithConfig:(SDImageCacheConfig *)config
 
 // shouldUseWeakMemoryCache: 爲 true 是將數保存到 weakCache, 反之不保存到 weakCache;獲取是爲 flase 是直接返回父類查出數據. 反之判斷內存中是否有對應數據,沒有再在 weakCache 獲取,並保存到內存.
 
 
 /// 保存數據 重寫父類方法,首先將數據保存內存,而後再將數據存儲在 weakCache.(weakCacheLock保證數據安全)
 /// 若是 shouldUseWeakMemoryCache 爲 false 則不存儲到 weakCache.
 - (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)g
 
 /// 獲取數據 重寫父類方法. 判斷是不是弱內存緩存,否: 直接返回父類查詢對象.是: 在 weakCache 取出相應對象並保存內存中返回.
 - (id)objectForKey:(id)key
 
 /// 根據 key 移除數據 重寫父類方法. 若是 shouldUseWeakMemoryCache 爲 false 只移除內存數據,爲 true 移除 weakCache.
 - (void)removeObjectForKey:(id)key
 
 /// 移除數據 重寫父類方法. 若是 shouldUseWeakMemoryCache 爲 false 只移除內存數據,爲 true 移除 weakCache.
 - (void)removeAllObjects 
 
複製代碼

SDImageCache

SDImageCache: 圖片緩存類github

  • memCache: SDMemoryCache 內存控制器
  • diskCachePath: NSString 根路徑
  • customPaths: NSMutableArray<NSString *> 自定義路徑 只讀路徑
  • ioQueue: dispatch_queue_t 數據讀取隊列
  • fileManager: NSFileManager 文件管理器
/// 提供單例方式建立
+ (nonnull instancetype)sharedImageCache


/// 給屬性複製初始化. 默認爲 NSCachesDirectory 下路徑. 添加兩個通知 刪除舊數據
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
diskCacheDirectory:(nonnull NSString *)directory

/// 省略一系列文件路徑處理方法...

/// 對 key MD5加密處理產生文件名
- (nullable NSString *)cachedFileNameForKey:(nullable NSString *)key

/** 緩存數據 self.config.shouldCacheImagesInMemory 爲true 則保存內存,不然只保存磁盤 @param image 圖片 @param imageData 圖片 Data @param key 鍵值 @param toDisk 是否存磁盤 @param completionBlock 存儲回調 */
- (void)storeImage:(nullable UIImage *)image imageData:(nullable NSData *)imageData forKey:(nullable NSString *)key toDisk:(BOOL)toDisk completion:(nullable SDWebImageNoParamsBlock)completionBlock

// 保存文件.經過寫文件形式保存. self.config.shouldDisableiCloud 爲 true 保存 iCloud
- (void)_storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key 

// 判斷是否有緩存文件存在
- (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock

// 獲取存儲的數據
- (nullable NSData *)diskImageDataForKey:(nullable NSString *)key

// 僅在內存查找緩存 
- (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key

// 在磁盤查找緩存 diskImage && self.config.shouldCacheImagesInMemory 爲 true 寫入內存
- (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key

// 緩存查找 先查找內存,而後磁盤
- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options done:(nullable SDCacheQueryCompletedBlock)doneBlock

// 移除緩存 (內存與磁盤)
- (void)removeImageForKey:(nullable NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(nullable SDWebImageNoParamsBlock)completion
複製代碼

UIImageView 加載圖片

UIImageView+WebCache

UIImageView 類擴展, 添加方法swift

/** UIImageView+WebCache 添加的方法最終都調用該方法. @param url 圖片 URL 地址 @param placeholder 佔位圖片 @param options 加載模式 @param progressBlock 進度 @param completedBlock 加載完成回調 */
- (void)sd_setImageWithURL:(nullable NSURL *)url laceholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock
複製代碼

UIView+WebCache

UIImageView,UIButton 等添加的類目方法將調用該擴展方法.緩存

/** * 該方法採用了大量的回調函數. * 1. 判斷當前圖片是否在加載,若是在加載取消加載. * 2. 設置佔位圖如今. * 3. 判斷 URL 是否有效,無效調用 completedBlock 返回 * 4. 初始化 SDWebImageManager 對象 * 5. 調用 SDWebImageManager 圖片加載方法. * 6. SDWebImageManager 回調中處理. * 6.1. 當前對象已銷燬或者回調圖片 URL 與須要加載不一致,返回 * 6.2. 判斷是否須要更新顯示圖片.調用 callCompletedBlockClojure 代碼. * 6.3. 須要更新圖片 調用圖片更換方法 * 6.4. 將 SDWebImageOperation 添加到 SDOperationsDictionary */
 
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options operationKey:(nullable NSString *)operationKey setImageBlock:(nullable SDSetImageBlock)setImageBlock progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock context:(nullable NSDictionary<NSString *, id> *)context

複製代碼

SDWebImageManager

圖片緩存查找以及下載處理安全

/** * 1. 建立 SDWebImageCombinedOperation * 2. 判斷 URL 是否有效.無效直接返回 * 3. 將 operation 加入到 runningOperations * 4. 調用 SDImageCache 中方法查找是否有緩存數據,若是找到回調 移除 Operation * 5. 找不到用 SDWebImageDownloader 下載圖片.並保存緩存中. - (id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDInternalCompletionBlock)completedBlock 複製代碼
相關文章
相關標籤/搜索