ios開發清除SDWebImage圖片緩存


一:一般在實際的項目應用中都會用到第三方框架SDWebImage去下載緩存圖片,但在ios開發應用中,常常涉及對SDWebImage緩存圖片的清除。本文所列出代碼即是對SDWebImage緩存圖片的清除。

二:代碼:

- (void)setGroup1 { RHCommonSection *section = [RHCommonSection commonSection]; section.header = @"第1組頭部"; section.footer = @"第1組尾部"; //清除SD的圖片緩存
    NSString *path = [SDImageCache sharedImageCache].diskCachePath; long long imageSize = [path fileSize]; NSString *fileSize = [NSString stringWithFormat:@"%.1f",imageSize/1024.0/1024.0]; RHSwitchItem *item1 = [RHSwitchItem commonItemWithTitle:@"清除緩存" icon:@"game_center"]; item1.subTitle = [NSString stringWithFormat:@"%@ M",fileSize]; self.item1 = item1; __weak typeof (item1) weakItem1 = item1; item1.cellBlock = ^(){ if (!path) return ; NSFileManager *manager = [NSFileManager defaultManager]; BOOL fileExits = [manager fileExistsAtPath:path]; if (fileExits) { [MBProgressHUD showHudWithMessage:@"正在清除緩存"]; [manager removeItemAtPath:path error:nil]; weakItem1.subTitle = nil; [self.tableView reloadData]; [MBProgressHUD hideHud]; } }; RHSwitchItem *item2 = [RHSwitchItem commonItemWithTitle:@"周邊" icon:@"near"]; RHSwitchItem *item3 = [RHSwitchItem commonItemWithTitle:@"應用" icon:@"app"]; section.itemsArr = @[item1,item2,item3]; [self.groups addObject:section]; } //回調時,調用block執行清除緩存操作
- (void)actionSheetViewController:(RHActionSheetViewController *)actionViewVC actionView:(RHActionSheetView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: [self clearMemory]; break; default: DLog(@"點擊了取消----------"); } } - (void)clearMemory { if (self.item1.cellBlock) { self.item1.cellBlock(); } }
//其中計算圖片緩存大小的操作,寫在了NSString的一個分類里,目的是:項目也許其他地方也會用到計算緩存的大小操作,所以將計算緩存大小的邏輯封裝起來

@interface NSString (Cqb) - (long long)fileSize; @end
#import "NSStrig+Cqb.h"

@implementation NSString (Cqb) - (long long)fileSize { NSFileManager *manager = [NSFileManager defaultManager]; //1:先判斷該路徑下是否存在緩存文件
 BOOL isDirectory = NO;//該文件目錄下是否存在文件夾,默認為NO BOOL fileExist = [manager fileExistsAtPath:self isDirectory:&isDirectory]; if (!fileExist) return 0; //2:判斷是文件還是文件夾
    
    if (isDirectory) {//是文件夾
    NSArray *subPaths = [manager contentsOfDirectoryAtPath:self error:nil]; long long totalFileSize = 0; for (NSString *path in subPaths) { NSString *fullPath = [self stringByAppendingPathComponent:path]; totalFileSize += [fullPath fileSize]; } return totalFileSize; }else { NSDictionary *attribute = [manager attributesOfItemAtPath:self error:nil]; return [attribute[NSFileSize] longLongValue]; } } @end

三:所用知識點:

1:NSString *path = [SDImageCache sharedImageCache].diskCachePath; 獲取path是SD緩存圖片的路徑,打印路徑,前往文件夾,如圖:

其中diskCachePath只sharedImageCache的.m中聲明了,滅有對外暴露接口,以屬性聲明diskCachePath,則在其.m文件中自動生成下划線成員變量,setter,getter方法,如果想獲得緩存路徑,需要在.h中提供getter方法。

/**
 *  項目中SD緩存圖片的路徑
 */
- (NSString*)diskCachePath;

2: long long imageSize = [path fileSize];將計算圖片緩存大小的方法封裝起來,為了項目中其他地方可用。為NSString寫一個分類,對象方法,返回字節數B,用long long 作為返回類型。計算緩存大小的思路為:1:將計算圖片緩存大小的方法封裝起來,為了項目中其他地方可用,因為可以獲得文件路徑,所以為NSString寫分類(對象或是類方法可供選擇,本文此處選擇對象方法,傳入path,返回long long 字節數)2:先判斷文件路徑是否存在,不存在直接retun 0;若存在,則先判斷該路徑下是否存在文件夾, BOOL isDirectory = NO;//該文件目錄下是否存在文件夾,默認為NO

BOOL fileExist = [manager fileExistsAtPath:self isDirectory:&isDirectory]; 3:若存在文件夾,則獲取該文件夾下所有文件的子路徑,並返回一個子路徑數組: NSArray *subPaths = [manager contentsOfDirectoryAtPath:self error:nil];遍歷數組,得到該文件夾下每一個子文件名,拼接路徑得到全路徑,4:定義totalSize,利用遞歸計算出文件的總大小並返回 long long totalFileSize = 0; for (NSString *path in subPaths) { NSString *fullPath = [self stringByAppendingPathComponent:path]; totalFileSize += [fullPath fileSize]; } return totalFileSize; 4:若為文件,直接計算出文件大小, NSDictionary *attribute = [manager attributesOfItemAtPath:self error:nil]; return [attribute[NSFileSize] longLongValue];

3:計算M: NSString *fileSize = [NSString stringWithFormat:@"%.1f",imageSize/1024.0/1024.0];%.1f為保留一位小數,為浮點類型,所以imageSize/1024.0/1024.0,1024也要表示為浮點型數據

4:清除緩存:利用block回調,調用block,思路:先判斷路徑是否存在,不存在直接返回,若存在,在判斷該路徑下文件是否存在,不存在直接返回,若存在則利用文件管理manager清除文件,在文件清除成功后,相應cell上的數據也要清除,賦值為nil,並要刷新表格才能顯示。此時刷新表格,也可以只刷數據改變的indexPath

5: __weak typeof (item1) weakItem1 = item1; 此處在block內部引起了循環引用,原因是在model數據中屬性定義了回調block,model對block有一個強引用,而block內部會對引用的變量有一個強引用,所以對model的對象item1也有一個強引用,則會造成循環引用,解決辦法:__weak typeof (item1) weakItem1 = item1

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM