很多app中都有清除緩存的功能袱巨,今天來說一下這種功能怎么操作,具體見如下代碼:
首先一般清除緩存之前會有一個提示飞蛹,是否清理緩存
#pragma mark -緩存處理
//清楚緩存調(diào)用
- (void)clearCacheAction {
UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"是否清理緩存"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"確定",nil];
alert.tag=CLEARCACHE_ALERT_TAG;
[alertshow];
}
然后寫提示的代理方法:
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(CLEARCACHE_ALERT_TAG== alertView.tag){
if(0== buttonIndex ){
}else{
[selfclearCache];
}
}
}
以下就是具體的清理緩存的方法了:
#pragma mark -
//清除緩存調(diào)用
- (void)clearCache {
[[[SDWebImageManagersharedManager]imageCache]clearDisk];
[[[SDWebImageManagersharedManager]imageCache]clearMemory];
[[CNToolsharedInstance]clearAllCache];
[selfpresentSuccessTips:@"緩存已清理"];
//修改顯示數(shù)據(jù)
NSIndexPath*indexPath = [NSIndexPathindexPathForRow:0inSection:1];
SELFSettingDefaultCell*cell = [self.tableViewcellForRowAtIndexPath:indexPath];
cell.subTitle.text= [selfcalculateCacheSize];
}
顯示出來的緩存大小是要計算的篡诽,計算方法如下:
//計算緩存大小
- (NSString*) calculateCacheSize {
NSIntegersize = [[SDImageCachesharedImageCache]getSize];
if(size <1024*1024*1024){//小于1G
CGFloataFloat = size/(1024*1024*1.0) ;
return[NSStringstringWithFormat:@"%.1fMB",aFloat];
}else{
CGFloataFloat = size/(1024*1024*1024*1.0) ;
return[NSStringstringWithFormat:@"%.1fG",aFloat];
}
}
通過以上代碼就可以實現(xiàn)清除緩存的功能了罕模,快來試試吧~