ios开发计算缓存的大小以及清除
2016-02-22 21:10
405 查看
-(void)myClearCacheAction{
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
, ^{
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,
YES)
objectAtIndex:0];
NSArray *files =
[[NSFileManager
defaultManager] subpathsAtPath:cachPath];
NSLog(@"files
:%lu",(unsigned long)[files
count]);
NSLog(@"缓存路径------------------%@",self.cachPath);
for (NSString
*p in
files) {
NSError *error;
NSString *path =
[cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager
defaultManager] fileExistsAtPath:path])
{
[[NSFileManager defaultManager]
removeItemAtPath:path error:&error];
}
}
[self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
}
//清理缓存
-(void)clearCacheSuccess
{
UIAlertController *alert =
[UIAlertController
alertControllerWithTitle:@"提示"
message:@"清理缓存成功"
preferredStyle: UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"确定"
style:UIAlertActionStyleDefault handler:^(UIAlertAction
* _Nonnull action)
{
}]];
//弹出提示框;
[self
presentViewController:alert animated:true
completion:nil];
}
//计算缓存大小
- (CGFloat)folderSizeAtPath:(NSString *)folderPath
{
NSFileManager *manager =
[NSFileManager
defaultManager];
if
(![manager fileExistsAtPath:folderPath]) {
return 0;
}
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
NSString *fileName
= nil;
long
long folderSize
= 0;
while
((fileName = [childFilesEnumerator
nextObject])
!= nil) {
NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize
+= [self fileSizeAtPath:fileAbsolutePath];
}
return
folderSize/(1024.0*1024.0);
}
- (long long)fileSizeAtPath:(NSString *)filePath
{
NSFileManager* manager =
[NSFileManager
defaultManager];
if
([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return
0;
}
$(".MathJax").remove();
<
4000
/script>
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
, ^{
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,
YES)
objectAtIndex:0];
NSArray *files =
[[NSFileManager
defaultManager] subpathsAtPath:cachPath];
NSLog(@"files
:%lu",(unsigned long)[files
count]);
NSLog(@"缓存路径------------------%@",self.cachPath);
for (NSString
*p in
files) {
NSError *error;
NSString *path =
[cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager
defaultManager] fileExistsAtPath:path])
{
[[NSFileManager defaultManager]
removeItemAtPath:path error:&error];
}
}
[self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
}
//清理缓存
-(void)clearCacheSuccess
{
UIAlertController *alert =
[UIAlertController
alertControllerWithTitle:@"提示"
message:@"清理缓存成功"
preferredStyle: UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"确定"
style:UIAlertActionStyleDefault handler:^(UIAlertAction
* _Nonnull action)
{
}]];
//弹出提示框;
[self
presentViewController:alert animated:true
completion:nil];
}
//计算缓存大小
- (CGFloat)folderSizeAtPath:(NSString *)folderPath
{
NSFileManager *manager =
[NSFileManager
defaultManager];
if
(![manager fileExistsAtPath:folderPath]) {
return 0;
}
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
NSString *fileName
= nil;
long
long folderSize
= 0;
while
((fileName = [childFilesEnumerator
nextObject])
!= nil) {
NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize
+= [self fileSizeAtPath:fileAbsolutePath];
}
return
folderSize/(1024.0*1024.0);
}
- (long long)fileSizeAtPath:(NSString *)filePath
{
NSFileManager* manager =
[NSFileManager
defaultManager];
if
([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return
0;
}
$(".MathJax").remove();
<
4000
/script>
相关文章推荐
- 关于c++浮点数的数字个数setprecision(n)与setiosflags(ios::fixed)
- iOS的23种设计模式简介
- iOS(学习8) 触摸
- IOS开发日志之CocoaPod的安装与简单使用Xcode7.2版本
- 【iOS】手势识别
- iOS开发手记-仿QQ音乐播放器动态歌词的实现
- iOS开发遇到 Missing iOS Distribution signing identity for XXXXXX
- iOS中ASI和AFN的区别
- iOS蓝牙开发(二):iOS连接外设的代码实现
- iOS蓝牙开发(一)蓝牙相关基础知识
- iOS TableView 默认选中某行
- iOS Runtime原理及使用
- iOS TextField改变placeholder颜色的方法
- GBK编码转NSString
- iOS Xcode工程目录的 folder 和 group的区别(蓝色和黄色文件夹的区别)
- ios 获取音乐库音乐相关
- iOS NSFileManager文件系统的管理
- iOS AutoLayout的用法
- iOS中使用AVPLayer自定义视频播放器
- iOS模拟器录屏转gif神器