SDWebImage快取機制

2022-08-30 09:30:12 字數 4089 閱讀 9523

存 取 刪 路徑

是在storeimage這個方法裡:

將儲存到記憶體和硬碟上

-(void)storeimage:(uiimage *)image recalculatefromimage:(bool)recalculate imagedata:(nsdata *)imagedata forkey:(nsstring *)key todisk:(bool)todisk 

// if memory cache is enabled

if (self.shouldcacheimagesinmemory)

if (todisk)

// 如果image是png格式,就是用uiimagepngrepresentation將其轉化為nsdata,否則按照jpeg格式轉化,並且壓縮質量為1,即無壓縮

if (imageispng)

else

#else

// 當然,如果不是在iphone平台上,就使用下面這個方法。不過不在我們研究範圍之內

data = [nsbitmapimagerep representationofimagerepsinarray:image.representations usingtype: nsjpegfiletype properties:nil];

#endif

}// 獲取到需要儲存的data後,下面就要用filemanager進行儲存了

if (data)

// 根據image的key(一般情況下理解為image的url)組合成最終的檔案路徑

// 上面那個生成的檔案路徑只是乙個檔案目錄,就跟/cache/images/img1.png和cache/images/的區別一樣

nsstring *cachepathforkey = [self defaultcachepathforkey:key];

// 這個url可不是網路端的url,而是file在系統路徑下的url

// 比如/foo/bar/baz --------> file:///foo/bar/baz

nsurl *fileurl = [nsurl fileurlwithpath:cachepathforkey];

// 根據儲存的路徑(cachepathforkey)和儲存的資料(data)將其存放到ios的檔案系統

[_filemanager createfileatpath:cachepathforkey contents:data attributes:nil];

// disable icloud backup

if (self.shoulddisableicloud)

}});

}}

記憶體快取使用nscache的objectforkey取資料:

- (uiimage *)imagefrommemorycacheforkey:(nsstring *)key
磁碟取資料 不斷用 datawithcontentsoffile來試資料是否在key對應的路徑中

- (uiimage *)imagefromdiskcacheforkey:(nsstring *)key 

// second check the disk cache...

uiimage *diskimage = [self diskimageforkey:key];

if (diskimage && self.shouldcacheimagesinmemory)

return diskimage;

}

removeimageforkeyfromdisk:withcompletion: // 非同步地將image從快取(記憶體快取以及可選的磁碟快取)中移除

clearmemory // 清楚記憶體快取上的所有image

cleardisk // 清除磁碟快取上的所有image

cleandisk // 清除磁碟快取上過期的image

看其中最長的乙個:

// 實現了乙個簡單的快取清除策略:清除修改時間最早的file

- (void)cleandiskwithcompletionblock:(sdwebimagenoparamsblock)completionblock

// 移除過期檔案

// 這裡判斷過期的方式:對比檔案的最後一次修改日期和expirationdate誰更晚,如果expirationdate更晚,就認為該檔案已經過期,具體解釋見上面

nsdate *modificationdate = resourcevalues[nsurlcontentmodificationdatekey];

if ([[modificationdate laterdate:expirationdate] isequaltodate:expirationdate])

// 計算當前已經使用的cache大小,

// 並將對應file的屬性存到cachefiles中

nsnumber *totalallocatedsize = resourcevalues[nsurltotalfileallocatedsizekey];

currentcachesize += [totalallocatedsize unsignedintegervalue];

[cachefiles setobject:resourcevalues forkey:fileurl];

}for (nsurl *fileurl in urlstodelete)

// 如果我們當前cache的大小已經超過了允許配置的快取大小,那就刪除已經快取的檔案。

// 刪除策略就是,首先刪除修改時間更早的快取檔案

if (self.maxcachesize > 0 && currentcachesize > self.maxcachesize) ];

// 每次刪除file後,就計算此時的cache的大小

// 如果此時的cache大小已經降到期望的大小了,就停止刪除檔案了

for (nsurl *fileurl in sortedfiles) }}

}// 如果有completionblock,就在主線程中呼叫

if (completionblock) );

}});}

// 簡單封裝了cachepathforkey:inpath

- (nsstring *)defaultcachepathforkey:(nsstring *)key

// cachepathforkey:inpath

- (nsstring *)cachepathforkey:(nsstring *)key inpath:(nsstring *)path

// cachedfilenameforkey:

- (nsstring *)cachedfilenameforkey:(nsstring *)key

// 使用了md5進行加密處理

// 開闢乙個16位元組(128位:md5加密出來就是128bit)的空間

unsigned char r[cc_md5_digest_length];

// 官方封裝好的加密方法

// 把str字串轉換成了32位的16進製制數列(這個過程不可逆轉) 儲存到了r這個空間中

cc_md5(str, (cc_long)strlen(str), r);

// 最終生成的檔名就是 "md5碼"+".檔案型別"

nsstring *filename = [nsstring stringwithformat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%@",

r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10],

r[11], r[12], r[13], r[14], r[15], [[key pathextension] isequaltostring:@""] ? @"" : [nsstring stringwithformat:@".%@", [key pathextension]]];

return filename;

}

SDWebImage的快取處理

使用sdwebimage請求資料,會產生相應的快取 這是用於顯示快取大小 呼叫方法 self filepath 即可獲得當前快取大小 顯示快取大小 float filepath 1 首先我們計算一下單個檔案的大小 longlong filesizeatpath nsstring filepath r...

SDWebImage清理 計算 快取

獲取快取個數 nsinteger diskcount sdimagecache sharedimagecache getdiskcount nslog zd diskcount 獲取快取大小 nsinteger diskszie sdimagecache sharedimagecache getsi...

SDWebImage 快取策略以及快取清理

1.快取策略 sdwebimageoptions 預設是開啟了硬碟 記憶體快取的 sdwebimagecachememoryonly 只快取到記憶體中,不快取到硬碟上 sdwebimageprogressivedownload 會一點一點慢慢顯示出來 就像瀏覽器顯示網頁上的一樣 sdwebimage...