码迷,mamicode.com
首页 > 移动开发 > 详细

ios 计算缓存大小

时间:2016-08-17 12:10:14      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

- (void)getSize2

{

    // 图片缓存

    NSUInteger size = [SDImageCache sharedImageCache].getSize;

    //  NSLog(@"%zd %@", size, NSTemporaryDirectory());

    

    NSFileManager *manager = [NSFileManager defaultManager];

    

    // 文件夹

    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    NSString *cachePath = [caches stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"];

    

    // 获得文件夹内部的所有内容

    //    NSArray *contents = [manager contentsOfDirectoryAtPath:cachePath error:nil];

    NSArray *subpaths = [manager subpathsAtPath:cachePath];

    NSLog(@"%@", subpaths);

}

 

- (void)getSize

{

    NSFileManager *manager = [NSFileManager defaultManager];

    

    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    NSString *cachePath = [caches stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"];

    

    NSDirectoryEnumerator *fileEnumerator = [manager enumeratorAtPath:cachePath];

    NSInteger totalSize = 0;

    for (NSString *fileName in fileEnumerator) {

        NSString *filepath = [cachePath stringByAppendingPathComponent:fileName];

        

//        BOOL dir = NO;

// 判断文件的类型:文件\文件夹

//        [manager fileExistsAtPath:filepath isDirectory:&dir];

//        if (dir) continue;

        NSDictionary *attrs = [manager attributesOfItemAtPath:filepath error:nil];

        if ([attrs[NSFileType] isEqualToString:NSFileTypeDirectory]) continue;

        

        totalSize += [attrs[NSFileSize] integerValue];

    }

  NSLog(@"%zd", totalSize);

}

 

ios 计算缓存大小

标签:

原文地址:http://www.cnblogs.com/soulDn/p/5779135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!