首页 > 移动平台 > 详细

iOS 获取本地缓存文件大小及清除

时间:2015-09-14 11:59:41      阅读:294      评论:0      收藏:0      [点我收藏+]

由于项目需求中要求计算出应用内的缓存文件的大小及清除工作,做了一个小小的模块提供给大家分享,随便说句买苹果还是选个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;

}

iOS 获取本地缓存文件大小及清除

原文:http://www.cnblogs.com/zero-zql/p/4806526.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!