iOS开发--清除缓存

很多app中都有清除缓存的功能,今天来说一下这种功能怎么操作,具体见如下代码:

首先一般清除缓存之前会有一个提示,是否清理缓存

#pragma mark -缓存处理

//清楚缓存调用

- (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 -

//清除缓存调用

- (void)clearCache {

[[[SDWebImageManagersharedManager]imageCache]clearDisk];

[[[SDWebImageManagersharedManager]imageCache]clearMemory];

[[CNToolsharedInstance]clearAllCache];

[selfpresentSuccessTips:@"缓存已清理"];

//修改显示数据

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];

}

}

通过以上代码就可以实现清除缓存的功能了,快来试试吧~

你可能感兴趣的:(iOS开发--清除缓存)