zoukankan      html  css  js  c++  java
  • iOS 获取本地缓存文件大小及清除

    由于项目需求中要求计算出应用内的缓存文件的大小及清除工作,做了一个小小的模块提供给大家分享,随便说句买苹果还是选个16g以上的那样妈妈就不会担心你的内存不用够用了哦! 

    // 清除本地缓存文件

    + (void)clearCacheFile

    {

        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];

        for (NSString *fileName in files)

        {

            NSError *error;

            NSString *path = [cachPath stringByAppendingPathComponent:fileName];

            if ([[NSFileManager defaultManager] fileExistsAtPath:path])

            {

                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];

            }

        }

    }

     

    // 计算本地缓存文件大小

    + (double)getCacheFileSize

    {

        NSFileManager *fileManager = [NSFileManager defaultManager];

        

        double fileSize = 0.0;

        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        NSArray *files = [fileManager subpathsAtPath:cachPath];

        for (NSString *fileName in files)

        {

            NSString *path = [cachPath stringByAppendingPathComponent:fileName];

            if ([fileManager fileExistsAtPath:path])

            {

                NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];

                fileSize += (double)([fileAttributes fileSize]);

            }

        }

        

        return fileSize;

    }

  • 相关阅读:
    C++ handle(句柄类) part2
    C++代理类的使用
    第一个blog
    C++ Handle(句柄) part1
    关于理想团队的构建和对软件流程的理解
    提供就医帮助的安卓APP
    上海地铁游移动APP需求分析
    关于学习了《构建之法》的若干存在疑惑的问题
    安卓APP开发简单实例 结对编程心得
    Vue修改Vue项目运行端口号(CLI2)
  • 原文地址:https://www.cnblogs.com/zero-zql/p/4806526.html
Copyright © 2011-2022 走看看