iOS 清理本地缓存
来源:互联网 发布:Js定义字符串编码 编辑:程序博客网 时间:2024/05/17 03:28
下面就以沙盒里面的零时文件tmp为清理对象;
-(void)func{
CGFloat size = [selffolderSizeAtPath:NSTemporaryDirectory()];
NSString *message = size > 1 ? [NSString stringWithFormat:@"缓存%.2fM, 删除缓存\n是否清除缓存?", size] : [NSStringstringWithFormat:@"缓存%.2fK, 删除缓存", size *1024.0];
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"提示"message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sureAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {
[selfcleanCaches:NSTemporaryDirectory()];
}];
UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:sureAction];
[selfpresentViewController:alertController animated:YEScompletion:nil];
}
#pragma mark- 计算缓存大小
// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path{
// 利用NSFileManager实现对文件的管理
NSFileManager *manager = [NSFileManagerdefaultManager];
CGFloat size = 0;
if ([manager fileExistsAtPath:path]) {
// 获取该目录下的文件,计算其大小
NSArray *childrenFile = [manager subpathsAtPath:path];
for (NSString *fileNamein childrenFile) {
NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
size += [manager attributesOfItemAtPath:absolutePatherror:nil].fileSize;
}
// 将大小转化为M
return size / 1024.0 / 1024.0;
}
return 0;
}
// 根据路径删除文件
- (void)cleanCaches:(NSString *)path{
// 利用NSFileManager实现对文件的管理
NSFileManager *fileManager = [NSFileManagerdefaultManager];
if ([fileManager fileExistsAtPath:path]) {
// 获取该路径下面的文件名
NSArray *childrenFiles = [fileManager subpathsAtPath:path];
for (NSString *fileNamein childrenFiles) {
// 拼接路径
NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
// 将文件删除
[fileManager removeItemAtPath:absolutePatherror:nil];
}
}
}
- iOS 清理本地缓存
- ios本地缓存及图片缓存清理
- iOS 计算SDWebImage本地缓存图片大小+清理本地缓存的图片
- ios清理缓存
- ios 清理缓存
- iOS 清理缓存
- ios 清理缓存
- iOS 清理缓存代码
- ios 清理缓存
- ios清理缓存
- iOS 清理缓存
- ios 清理缓存
- ios 清理cach缓存
- iOS 清理缓存方法
- iOS APP缓存清理
- iOS 缓存大小 清理
- ios-清理应用缓存
- iOS-Swift-清理缓存
- Kotlin学习笔记--函数、函数表达式、默认参数、具名参数
- 【深度学习】Network In Network--在CNN中构建MLP
- Java设计模式--观察者模式
- 0x000.索引
- 洛谷 P1414 又是毕业季II
- iOS 清理本地缓存
- OpenStack环境搭建(1)--控制节点
- Java sql查询
- linux中mysql的安装
- Android生命周期浅析(一)
- 延迟加载(Lazyload)三种实现方式
- VUE组件之间的通信
- opencv对遥感图像基本操作
- mysql-5.7.20-winx64.zip 安装