iOS 应用文件缓存一般分为2种情况:用户自己缓存,第三方库SDWebImage的缓存;
对缓存大小的统计主要依赖于iOS提供的类NSFileManager来做;
一、单个文件计算大小
-(long long)fileSizeAtPath:(NSString *)path{
NSFileManager *fileManager=[NSFileManager defaultManager];
if([fileManager fileExistsAtPath:path]){
long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
return size;
}
return 0;
}
二、文件夹大小计算
-(float)folderSizeAtPath:(NSString *)path{
NSFileManager *fileManager=[NSFileManager defaultManager];
NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
cachePath=[cachePath stringByAppendingPathComponent:path];
long long folderSize=0;
if ([fileManager fileExistsAtPath:cachePath])
{
NSArray *childerFiles=[fileManager subpathsAtPath:cachePath];
for (NSString *fileName in childerFiles)
{
NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName];
long long size=[self fileSizeAtPath:fileAbsolutePath];
folderSize += size;
NSLog(@"fileAbsolutePath=%@",fileAbsolutePath);
}
//SDWebImage框架自身计算缓存的实现
folderSize+=[[SDImageCache sharedImageCache] getSize];
return folderSize/1024.0/1024.0;
}
return 0;
}
三、文件清除
1、用户自身文件大小清除
利用NSFileManager API进行文件操作
-(void)clearCache:(NSString *)path{
NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
cachePath=[cachePath stringByAppendingPathComponent:path];
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:cachePath]) {
NSArray *childerFiles=[fileManager subpathsAtPath:cachePath];
for (NSString *fileName in childerFiles) {
//如有需要,加入条件,过滤掉不想删除的文件
NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName];
NSLog(@"fileAbsolutePath=%@",fileAbsolutePath);
[fileManager removeItemAtPath:fileAbsolutePath error:nil];
}
}
}
2、SDWebImage框架自己实现了清理缓存操作,我们可以直接调用。
-(void)clearCacheSDImageCache {
[[SDImageCache sharedImageCache] cleanDisk];
}
接口说明
1、OC中获取文件属性(Attributes)的方法,获得所给路径(path)的所有“文件属性(fileAttributes),返回一个字典(NSDictionary)来存储,即以键值对的形式存储;
- (nullable NSDictionary<NSFileAttributeKey, id> *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
返回属性字典的属性列表:
//如下属性可以使用点语法获取
Printing description of dic:
{
NSFileCreationDate = "2016-10-18 10:12:47 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 501;
NSFileGroupOwnerAccountName = mobile;
NSFileModificationDate = "2016-10-18 10:12:48 +0000";
NSFileOwnerAccountID = 0;
NSFileOwnerAccountName = root;
NSFilePosixPermissions = 420;
NSFileProtectionKey = NSFileProtectionNone;
NSFileReferenceCount = 1;
NSFileSize = 12624777;
NSFileSystemFileNumber = 6841436;
NSFileSystemNumber = 16777219;
NSFileType = NSFileTypeRegular;
}
2、OC中获取文件系统属性
获得所给路径(path)所在“文件系统的属性,返回一个字典(NSDictionary)来存储,即以键值对的形式存储
- (nullable NSDictionary<NSFileAttributeKey, id> *)attributesOfFileSystemForPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
返回属性字典的属性列表:
Printing description of dicc:
{
NSFileSystemFreeNodes = 903996;
NSFileSystemFreeSize = 3702767616;
NSFileSystemNodes = 3128248;
NSFileSystemNumber = 16777219;
NSFileSystemSize = 12813312000;
}
//model
//获得所给文件路径所在文件系统的属性
NSDictionary *attrs = [fm attributesOfFileSystemForPath:file error:nil];
//取出文件系统的属性中NSFileSystemSize键所对应的值,即系统硬盘已经存储的大小
NSNumber *systemSize = attrs[NSFileSystemSize];
NSNumber *systemFreeSize = attrs[NSFileSystemFreeSize];
文件大小获取方式的优化方案
//需要引入头文件
#import <sys/stat.h>
- (void)fileSize {
struct stat statbuf;
const char *cpath = [@"pa" UTF8String];
if (cpath && stat(cpath, &statbuf) == 0) {
NSNumber *fileSize = [NSNumber numberWithUnsignedLongLong:statbuf.st_size];
NSDate *modificationDate = [NSDate dateWithTimeIntervalSince1970:statbuf.st_mtime];
NSDate *creationDate = [NSDate dateWithTimeIntervalSince1970:statbuf.st_ctime];
// etc
}
}
ios 设备磁盘剩余空间统计
/// Free disk space in bytes.
static int64_t _KKeyDiskSpaceFree() {
NSError *error = nil;
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) return -1;
int64_t space = [[attrs objectForKey:NSFileSystemFreeSize] longLongValue];
if (space < 0) space = -1;
return space;
}