iOS清除缓存机制
来源:互联网 发布:陕西大数据集团怎么样 编辑:程序博客网 时间:2024/06/03 20:08
应用程序中基本上都要有清除缓存功能,下面是自己写的一个清除缓存的事例,有需要的可以看下。大神勿喷,谢谢!
效果图如下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];if (cell == nil){ cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TableCell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.detailTextLabel.font=[UIFont systemFontOfSize:14]; }if (indexPath.row==0) { cell.textLabel.text = @"清理缓存"; cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2fM",[self filePath]];}else{ cell.imageView.image = [UIImage imageNamed:@"206040274942060663"];}return cell;
}}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES];if (indexPath.row==0) { //清除缓存 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"是否清理缓存?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alertView show]; } }// 显示缓存大小-( float )filePath{NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];return [ self folderSizeAtPath :cachPath]; } //1:首先我们计算一下 单个文件的大小 - ( long long ) fileSizeAtPath:( NSString *) filePath{ NSFileManager * manager = [ NSFileManager defaultManager ]; if ([manager fileExistsAtPath :filePath]){ return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ]; } return 0 ;}//2:遍历文件夹获得文件夹大小,返回多少 M- ( float ) folderSizeAtPath:( NSString *) folderPath{NSFileManager * manager = [ NSFileManager defaultManager ];if (![manager fileExistsAtPath :folderPath]) return 0 ;NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];NSString * fileName;long long folderSize = 0 ; while ((fileName = [childFilesEnumerator nextObject ]) != nil ){ NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName]; folderSize += [ self fileSizeAtPath :fileAbsolutePath]; } return folderSize/( 1024.0 * 1024.0 );}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 1 ) { [self clearFile]; }}// 清理缓存- (void)clearFile{ dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) , ^{//地址 NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ]; NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath]; NSLog ( @"cachpath = %@" , cachPath); for ( NSString * p in files) { NSError * error = nil ; NSString * path = [cachPath stringByAppendingPathComponent :p]; if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) { [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error]; } } [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];}); } -(void)clearCachSuccess{ NSLog ( @" 清理成功 " ); NSIndexPath *index=[NSIndexPath indexPathForRow:0 inSection:0];//刷新某一行数据[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];}
代码通道:https://github.com/Gang679/GZBase 记得星一下,谢谢!
0 0
- iOS清除缓存机制
- ios-SDWebImage清除缓存机制
- ios webview清除缓存。
- ios webview清除缓存
- iOS开发清除缓存
- ios webview清除缓存
- ios webview清除缓存
- ios webview清除缓存
- IOS清除缓存
- iOS清除缓存
- 清除ios的缓存
- iOS 清除缓存
- iOS 清除缓存
- iOS 清除缓存
- iOS清除缓存
- iOS清除缓存
- iOS 清除缓存
- iOS 清除缓存
- GIF图像格式介绍
- Java EE 导入Maven项目报错
- 二进制、十进制、十六进制转换的算法
- 17.视图
- Java面试笔试题大汇总(最全+详细答案)
- iOS清除缓存机制
- 结构体指针
- javascript-MDN笔记-产生固定范围随机数
- MySQL 中怎么把时间戳转换成正常时间
- Oracle存储过程与函数
- HDU2532 Engine 字符串+模拟
- codeforces 727F. Polycarp's problems DP or 二分预处理答案
- Javascript—②函数
- cvRound, cvFloor, cvCeil (opencv浮点类型取整函数)