標籤:
存 取 刪 路徑
1.1 存
是在storeImage這個方法裡:
將圖片儲存到記憶體和硬碟上
-(void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk {if (!image || !key) { return;}// if memory cache is enabledif (self.shouldCacheImagesInMemory) { NSUInteger cost = SDCacheCostForImage(image); [self.memCache setObject:image forKey:key cost:cost];}if (toDisk) { dispatch_async(self.ioQueue, ^{ NSData *data = imageData; // 如果image存在,但是需要重新計算(recalculate)或者data為空白 // 那就要根據image重建新的data // 不過要是連image也為空白的話,那就別存了 if (image && (recalculate || !data)) {#if TARGET_OS_IPHONE // 我們需要判斷image是PNG還是JPEG // PNG的圖片很容易檢測出來,因為它們有一個特定的標示 (http://www.w3.org/TR/PNG-Structure.html) // PNG圖片的前8個位元組不許符合下面這些值(十進位表示) // 137 80 78 71 13 10 26 10 // 如果imageData為空白l (舉個例子,比如image在下載後需要transform,那麼就imageData就會為空白) // 並且image有一個alpha通道, 我們將該image看做PNG以避免透明度(alpha)的丟失(因為JPEG沒有透明色) int alphaInfo = CGImageGetAlphaInfo(image.CGImage);// 擷取image中的透明資訊 // 該image中確實有透明資訊,就認為image為PNG BOOL hasAlpha = !(alphaInfo == kCGImageAlphaNone || alphaInfo == kCGImageAlphaNoneSkipFirst || alphaInfo == kCGImageAlphaNoneSkipLast); BOOL imageIsPng = hasAlpha; // 但是如果我們已經有了imageData,我們就可以直接根據data中前幾個位元組判斷是不是PNG if ([imageData length] >= [kPNGSignatureData length]) { // ImageDataHasPNGPreffix就是為了判斷imageData前8個位元組是不是符合PNG標誌 imageIsPng = ImageDataHasPNGPreffix(imageData); } // 如果image是PNG格式,就是用UIImagePNGRepresentation將其轉化為NSData,否則按照JPEG格式轉化,並且壓縮品質為1,即無壓縮 if (imageIsPng) { data = UIImagePNGRepresentation(image); } else { data = UIImageJPEGRepresentation(image, (CGFloat)1.0); }#else // 當然,如果不是在iPhone平台上,就使用下面這個方法。不過不在我們研究範圍之內 data = [NSBitmapImageRep representationOfImageRepsInArray:image.representations usingType: NSJPEGFileType properties:nil];#endif } // 擷取到需要儲存的data後,下面就要用fileManager進行儲存了 if (data) { // 首先判斷disk cache的檔案路徑是否存在,不存在的話就建立一個 // disk cache的檔案路徑是儲存在_diskCachePath中的 if (![_fileManager fileExistsAtPath:_diskCachePath]) { [_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL]; } // 根據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) { [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil]; } } });}}
1.2 取
記憶體緩衝使用NSCache的objectForKey取資料:
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key { return [self.memCache objectForKey:key];}
磁碟取資料 不斷用 dataWithContentsOfFile來試資料是否在key對應的路徑中
- (UIImage *)imageFromDiskCacheForKey:(NSString *)key {// First check the in-memory cache...UIImage *image = [self imageFromMemoryCacheForKey:key];if (image) { return image;}// Second check the disk cache...UIImage *diskImage = [self diskImageForKey:key];if (diskImage && self.shouldCacheImagesInMemory) { NSUInteger cost = SDCacheCostForImage(diskImage); [self.memCache setObject:diskImage forKey:key cost:cost];}return diskImage;}
1.3 刪
- removeImageForKeyfromDisk:withCompletion: // 非同步地將image從緩衝(記憶體緩衝以及可選的磁碟緩衝)中移除
- clearMemory // 清楚記憶體緩衝上的所有image
- clearDisk // 清除磁碟緩衝上的所有image
- cleanDisk // 清除磁碟緩衝上到期的image
看其中最長的一個:
// 實現了一個簡單的緩衝清除策略:清除修改時間最早的file- (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock {dispatch_async(self.ioQueue, ^{ // 這兩個變數主要是為了下面產生NSDirectoryEnumerator準備的 // 一個是記錄遍曆的檔案目錄,一個是記錄遍曆需要預先擷取檔案的哪些屬性 NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES]; NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey]; // 遞迴地遍曆diskCachePath這個檔案夾中的所有目錄,此處不是直接使用diskCachePath,而是使用其產生的NSURL // 此處使用includingPropertiesForKeys:resourceKeys,這樣每個file的resourceKeys對應的屬性也會在遍曆時預先擷取到 // NSDirectoryEnumerationSkipsHiddenFiles表示不遍曆隱藏檔案 NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL includingPropertiesForKeys:resourceKeys options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:NULL]; // 擷取檔案的到期時間,SDWebImage中預設是一個星期 // 不過這裡雖然稱*expirationDate為到期時間,但是實質上並不是這樣。 // 其實是這樣的,比如在2015/12/12/00:00:00最後一次修改檔案,對應的到期時間應該是 // 2015/12/19/00:00:00,不過現在時間是2015/12/27/00:00:00,我先將目前時間減去1個星期,得到 // 2015/12/20/00:00:00,這個時間才是我們函數中的expirationDate。 // 用這個expirationDate和最後一次修改時間modificationDate比較看誰更晚就行。 NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge]; // 用來儲存對應檔案的一些屬性,比如檔案所需磁碟空間 NSMutableDictionary *cacheFiles = [NSMutableDictionary dictionary]; // 記錄當前已經使用的磁碟緩衝大小 NSUInteger currentCacheSize = 0; // 在緩衝的目錄開始遍曆檔案. 此次遍曆有兩個目的: // // 1. 移除到期的檔案 // 2. 同時儲存每個檔案的屬性(比如該file是否是檔案夾、該file所需磁碟大小,修改時間) NSMutableArray *urlsToDelete = [[NSMutableArray alloc] init]; for (NSURL *fileURL in fileEnumerator) { NSDictionary *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:NULL]; // 當前掃描的是目錄,就跳過 if ([resourceValues[NSURLIsDirectoryKey] boolValue]) { continue; } // 移除到期檔案 // 這裡判斷到期的方式:對比檔案的最後一次修改日期和expirationDate誰更晚,如果expirationDate更晚,就認為該檔案已經到期,具體解釋見上面 NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey]; if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) { [urlsToDelete addObject:fileURL]; continue; } // 計算當前已經使用的cache大小, // 並將對應file的屬性存到cacheFiles中 NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey]; currentCacheSize += [totalAllocatedSize unsignedIntegerValue]; [cacheFiles setObject:resourceValues forKey:fileURL]; } for (NSURL *fileURL in urlsToDelete) { // 根據需要移除檔案的url來移除對應file [_fileManager removeItemAtURL:fileURL error:nil]; } // 如果我們當前cache的大小已經超過了允許配置的緩衝大小,那就刪除已經緩衝的檔案。 // 刪除策略就是,首先刪除修改時間更早的快取檔案 if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize) { // 直接將當前cache大小降到允許最大的cache大小的一般 const NSUInteger desiredCacheSize = self.maxCacheSize / 2; // 根據檔案修改時間來給所有快取檔案排序,按照修改時間越早越在前的規則排序 NSArray *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(id obj1, id obj2) { return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]]; }]; // 每次刪除file後,就計算此時的cache的大小 // 如果此時的cache大小已經降到期望的大小了,就停止刪除檔案了 for (NSURL *fileURL in sortedFiles) { if ([_fileManager removeItemAtURL:fileURL error:nil]) { // 擷取該檔案對應的屬性 NSDictionary *resourceValues = cacheFiles[fileURL]; // 根據resourceValues擷取該檔案所需磁碟空間大小 NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey]; // 計算當前cache大小 currentCacheSize -= [totalAllocatedSize unsignedIntegerValue]; if (currentCacheSize < desiredCacheSize) { break; } } } } // 如果有completionBlock,就在主線程中調用 if (completionBlock) { dispatch_async(dispatch_get_main_queue(), ^{ completionBlock(); }); }});}
1.4 圖片儲存路徑
// 簡單封裝了cachePathForKey:inPath- (NSString *)defaultCachePathForKey:(NSString *)key { return [self cachePathForKey:key inPath:self.diskCachePath];}// cachePathForKey:inPath- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path {// 根據傳入的key建立最終要儲存時的檔案名稱NSString *filename = [self cachedFileNameForKey:key];// 將儲存的檔案路徑和檔案名稱綁定在一起,作為最終的儲存路徑return [path stringByAppendingPathComponent:filename];}// cachedFileNameForKey:- (NSString *)cachedFileNameForKey:(NSString *)key {const char *str = [key UTF8String];if (str == NULL) { str = "";}// 使用了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的緩衝機制