第一种方法
NSFileManager *manger = [NSFileManager defaultManager];
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
NSString *cachePath = [caches stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"];
NSDirectoryEnumerator *fileEnumerator = [manger enumeratorAtPath:cachePath];
NSInteger totalSize = 0;
for (NSString *fileName in fileEnumerator) {
NSString *filePath = [cachePath stringByAppendingPathComponent:fileName];
NSDictionary *attrs = [manger attributesOfItemAtPath:filePath error:nil];
if([attrs[NSFileType]isEqualToString:NSFileTypeDirectory]) {
continue;
}
totalSize += [attrs[NSFileSize] integerValue];
}
第二种方法
NSFileManager *manager = [NSFileManager defaultManager];
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *cachePath = [caches stringByAppendingPathComponent:@"qqq"];
NSArray *subpaths = [manager subpathsAtPath:cachePath];
NSInteger totalSize = 0;
for (NSString *fileName in subpaths) {
NSString *filepath = [cachePath stringByAppendingPathComponent:fileName];
NSDictionary *attrs = [manager attributesOfItemAtPath:filepath error:nil];
if ([attrs[NSFileType] isEqualToString:NSFileTypeDirectory]) continue;
totalSize += [attrs[NSFileSize] integerValue];
}