IOS 获取缓存的大小

    1. 使用SDWebImage自带的方法实现
  //sdWebImage自带的计算缓存大小的方法
    NSUInteger cacheNumber = [SDImageCache sharedImageCache].getSize;
    BDJLog(@"缓存大小%lu", cacheNumber);
  • 2.利用NSFileManager计算文件的的大小
// NSFileManager
    // attributesOfItemAtPath:指定文件路径,就能获取文件属性
    // 把所有文件尺寸加起来
    
    // 获取Caches文件夹路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    // 获取default文件路径
    NSString *defaultPath = [cachePath stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default/0ccb23cf1c79acaa461c1a43cddc6759.png"];
    
    // 遍历文件夹所有文件,一个一个加起来
    
    // 获取文件管理者
   NSFileManager *mgr = [NSFileManager defaultManager];
    
    // 获取文件属性
    // attributesOfItemAtPath:只能获取文件尺寸,获取文件夹不对,
   NSDictionary *attr = [mgr attributesOfItemAtPath:defaultPath error:nil];
    
    // default尺寸
   NSInteger fileSize = [attr fileSize];
    
    NSLog(@"图片的大小 = %ld",fileSize);
  • 利用FileManager删除缓存
- (NSString *)getSizeString
{
    NSInteger totalSize = [self getMyFileSize:CachePath];
    NSString *str = @"清除缓存";
    if (totalSize > 1000 * 1000)
    {
        CGFloat fileSize = totalSize / 1000.0/1000.0;
        str = [NSString stringWithFormat:@"%@(%.1fMB)", str, fileSize];
    }
    else if (totalSize > 1000 )
    {
        CGFloat fileSize = totalSize / 1000.0;
        str = [NSString stringWithFormat:@"%@(%.1fKB)", str, fileSize];
    }
    else if (totalSize > 0)
    {
        CGFloat fileSize = totalSize;
        str = [NSString stringWithFormat:@"%@(%.1fdB)", str, fileSize];
    }
    
    return str;
}

//自己去实现SDWebImage的底层

-(NSInteger)getMyFileSize:(NSString *)directoryPath
{
    NSFileManager *mgr = [NSFileManager defaultManager];
    //获取文件下所有的子路径
    NSArray *subPaths = [mgr subpathsAtPath:directoryPath];
    BDJLog(@"%@", subPaths);
    //遍历他的子路径
    NSInteger totalSize = 0;
    for (NSString *subPath in subPaths)
    {
        NSString *fileCurrentPath = [directoryPath stringByAppendingPathComponent:subPath];
        //判断是否是隐藏文件
        if ([fileCurrentPath containsString:@".DS"]) continue;
        //判断是否为文件夹
        BOOL isDirectory;
        //判断文件是否存在
        BOOL isExist = [mgr fileExistsAtPath:fileCurrentPath isDirectory:&isDirectory];

        if (!isExist || isDirectory) continue;
        //获取文件的属性
        NSDictionary *attri = [mgr attributesOfItemAtPath:fileCurrentPath error:nil];
        //attributesOfItemAtPath:只能获取文件尺寸,获取文件夹不对,
        NSInteger fileSize = [attri fileSize];
        totalSize += fileSize;
    }

    BDJLog(@"%ld", totalSize);

    return totalSize;
}
- 删除操作 -

{
    NSFileManager *mgr = [NSFileManager defaultManager];
    NSArray *subPaths = [mgr subpathsAtPath:CachePath];
    
    for (NSString *subPath in subPaths)
    {
        NSString * deleteStr = [CachePath stringByAppendingPathComponent:subPath];
        [mgr removeItemAtPath:deleteStr error:nil];
    }
    [self.tableView reloadData];
}

如果这个功能我们经常使用可以封装工具类
计算过程如果太多的话 会造成手机太卡,所有我们需要加入子线程
//完整实现,不能让计算文件的过程影响手机的流畅

+ (void)getFileSize:(NSString *)directoryPath completion:(void (^)(NSInteger))completion
{
   
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    BOOL isDirectory;
    
    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
    
    if (!isExist||!isDirectory)
    {
        // 抛异常
        // name:异常名称
        // reason:报错原因
        NSException *excp = [NSException exceptionWithName:@"pathError" reason:@"经输入正确的文件的路径" userInfo:nil];
        //抛出
        [excp raise];
    }
    // NSFileManager
    // attributesOfItemAtPath:指定文件路径,就能获取文件属性
    // 把所有文件尺寸加起来
    dispatch_async(dispatch_get_global_queue(0, 0), ^
    {
       
        
        NSArray *subPaths = [mgr subpathsAtPath:directoryPath];
        NSInteger totalSize = 0;
        
        for (NSString *subPath in subPaths)
        {
            NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
            //判断是否为隐藏文件
            if ([filePath containsString:@".DS"]) continue;
            //判断是否为文件
            BOOL isDirectory;
            //判断文件是否存在
            BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
            
            if (!isExist||!isDirectory) continue;
            
            NSDictionary *fileDic = [mgr attributesOfItemAtPath:filePath error:nil];
            NSInteger fileSize = [fileDic fileSize];
            totalSize +=fileSize;
            
        }
        
        //计算完成,返回主线程
        dispatch_sync(dispatch_get_main_queue(), ^
        {
            if (completion)
            {
                completion(totalSize);
            }
        });
        
    });
    
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容