iOS经典讲解之清除缓存文件
来源:互联网 发布:电脑有声阅读软件 编辑:程序博客网 时间:2024/05/17 23:02
作者:Loving_iOS
转载请标明出处:http://blog.csdn.net/loving_ios/article/details/49251321清除缓存之前先要计算文件的大小:
// 计算目录大小- (CGFloat)folderSizeAtPath:(NSString *)path{// 利用NSFileManager实现对文件的管理</span> NSFileManager *manager = [NSFileManager defaultManager]; CGFloat size = 0; if ([manager fileExistsAtPath:path]) {// 获取该目录下的文件,计算其大小 NSArray *childrenFile = [manager subpathsAtPath:path]; for (NSString *fileName in childrenFile) { NSString *absolutePath = [path stringByAppendingPathComponent:fileName]; size += [manager attributesOfItemAtPath:absolutePath error:nil].fileSize; }// 将大小转化为M return size / 1024.0 / 1024.0; } return 0;
// 根据路径删除文件- (void)cleanCaches:(NSString *)path{ // 利用NSFileManager实现对文件的管理 NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:path]) { // 获取该路径下面的文件名 NSArray *childrenFiles = [fileManager subpathsAtPath:path]; for (NSString *fileName in childrenFiles) { // 拼接路径 NSString *absolutePath = [path stringByAppendingPathComponent:fileName];// 将文件删除 [fileManager removeItemAtPath:absolutePath error:nil]; } }}
计算沙盒中文件的大小并删除沙盒中文件的例子:
- (void)cleanCaches{ [self cleanCaches:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject]; [self cleanCaches:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject]; [self cleanCaches:NSTemporaryDirectory()];}// 清除缓存 CGFloat size = [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSTemporaryDirectory()]; NSString *message = size > 1 ? [NSString stringWithFormat:@"缓存%.2fM, 删除缓存", size] : [NSString stringWithFormat:@"缓存%.2fK, 删除缓存", size * 1024.0]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) { [self cleanCaches]; }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]; [alert addAction:action]; [alert addAction:cancel]; [self showDetailViewController:alert sender:nil];
1 0
- iOS经典讲解之清除缓存文件
- ios-清除文件缓存
- iOS之清除缓存
- IOS 清除APP缓存文件
- iOS 开发之缓存清除
- IOS 之 计算缓存,清除缓存
- iOS经典讲解之UICollectionView
- iOS经典讲解之NSUserDefaults
- iOS清除xcode缓存和生成文件
- iOS 清除xcode缓存和生成文件
- iOS-缓存清除以及文件操作
- iOS 文件管理器(文件操作, 缓存计算, 清除缓存)
- iOS开发之清除Alamofire缓存
- iOS获取缓存文件的大小并清除缓存
- iOS经典讲解之妙用UITextView
- iOS经典讲解之NSNotification(通知)
- iOS经典讲解之Swift数据类型详解
- iOS经典讲解之Socket使用教程
- Hadoop学习,第一篇
- 关于/dev/fd/
- 一、ios的通过Xib构建UICollectionView的简单运用
- jquery 实现 input 内容全选
- hudson 持续集成 编译代码出现 “找不到符号”
- iOS经典讲解之清除缓存文件
- 回退 服务器 上的 git 仓库 & 删除 服务器上的 git 分支
- 第六周项目 建立顺序栈算法库
- 程序员们为什么不能“面朝大海,春暖花开?”
- Longest Palindromic Substring
- java调试程序中每一步所用时间工具类
- 第6周 项目3-括号的匹配
- install pip on osx
- 第六周项目二—建立链栈算法库