简单的清除缓存功能

向大家介绍下,自己写得简单地清除缓存功能,可能会有些用,可以帮助大家, 那样我是最开心的.也可能还有很多缺陷问题,希望大家给些建议.让我改正,我同样开心

首先创建一个UIAlertController,来显示缓存大小,还有是否清除

8F65C9B2-172D-4E63-BEE4-1753602F71ED.png
// 创建UIAlertController
- (void)createAlertController
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"是否清除缓存" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self ClearCache];
    }];
    [alertController addAction:actionCancel];
    [alertController addAction:actionOk];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

下面就是计算缓存的内容

// 计算清除的缓存

- (void)CalculateCache
{
    //找到缓存路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
   //folderSizeAtPath这个方法是遍历文件夹获得文件夹大小,下面封装的.
    float size = [self folderSizeAtPath:path];
    self.strOfCache = [NSString stringWithFormat:@"缓存:%.1fM", size];
   
    [self.tableView reloadData];
}

// 遍历文件夹获得文件夹大小, 返回多少m
- (float) folderSizeAtPath:(NSString *)folderPath
{
    //创建文件管理对象,
    NSFileManager *manager = [NSFileManager defaultManager];
    // 判断路径是否存在
    if (![manager fileExistsAtPath:folderPath])
    {
        return 0;
    }
    else
    {
        NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
        NSString *fileName;
        // 用来累加缓存的folderSize
        long long folderSize = 0;
        while ((fileName = [childFilesEnumerator nextObject]) != nil)
        {
            NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
            //fileSizeAtPath方法是计算单个文件的大小, 下面封装
            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];

    }else{
        return 0;
    }
}
// 清除缓存
- (void)ClearCache
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
    //遍历清除每个文件缓存
    for (NSString *p in files)
    {
        NSError *error;
        NSString *Path = [path stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path])
        {
            [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
        }
    }
    //重写计算缓存
    [self CalculateCache];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,462评论 25 708
  • 周五了,终于可以休息了,其实周末并不一定得到休息,除了哄娃还要收拾屋子,还想学习逛街购物做饭等等……为什么还是盼着...
    妞妞yang阅读 225评论 0 0
  • 一声疾雷惊天地, 风雨磅礴三千尺。 盼君洗尽人间事, 浩然正气天地间。
    青色的山梁阅读 253评论 0 0
  • pom.xml中添加依赖jar包,在src/test/java下新建Test文件。 编辑啊pom.xml文件 所有...
    静jingjing阅读 397评论 0 1