美文网首页
知识点总结28:计算缓存文件的大小

知识点总结28:计算缓存文件的大小

作者: 枫之叶_小乙哥 | 来源:发表于2017-02-10 02:21 被阅读69次

计算缓存的方法:(用数组或者遍历器来保存子路径)

  • 一.用数组保存所有子路径

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = @"清除缓存";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        unsigned long long size = [self getSize];
        ZGKLog(@"size = %llu", size);
    };
}


- (unsigned long long)getSize{
    // 0.获得缓存文件路径
    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    ZGKLog(@"cachesPath: %@", cachesPath);
    NSString *dirPath = [cachesPath stringByAppendingPathComponent:@"default"];
    
    // 1.文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    
    // 2.获得文件夹得属性
    // 注意1: 获得文件夹的一些属性资料(NSFileSize如果有子集目录,则文件大小不为NSFileSize,如果只有一个subPath才是该文件的大小,因为文件夹没有NSFileSize记录整个文件的大小,都是要通过遍历子目录计算的)
    NSDictionary *attrs = [manager attributesOfItemAtPath:dirPath error:nil];
    /*
    打印attires的结果:
     NSFileCreationDate = "2017-02-07 17:15:11 +0000";
     NSFileExtensionHidden = 0;
     NSFileGroupOwnerAccountID = 20;
     NSFileGroupOwnerAccountName = staff;
     NSFileModificationDate = "2017-02-09 17:56:09 +0000";
     NSFileOwnerAccountID = 501;
     NSFilePosixPermissions = 493;
     NSFileReferenceCount = 4;
     NSFileSize = 136;
     NSFileSystemFileNumber = 22754319;
     NSFileSystemNumber = 16777220;
     NSFileType = NSFileTypeDirectory;
     */
    ZGKLog(@"attrs :\n %@", attrs);
    
     // 注意:NSFilemanager的contentsOfDirectoryAtPath:和subpathsAtPath:方法的异同

    // 获得文件夹一级目录
    NSArray *contentPaths = [manager contentsOfDirectoryAtPath:dirPath error:nil];
    NSLog(@"contents = %@", contentPaths);
    
    // 2.获得文件夹所有子级目录(用数组获得所有子路径)
    NSArray *subPaths = [manager subpathsAtPath:dirPath];
    NSLog(@"subPaths = %@", subPaths);

    // 3.计算所有子级目录的大小(注意:在mac系统下1M = 1000KB,而不是1024)
    unsigned long long size = 0;
    for (NSString *subPath in subPaths) {
        // 全路径
        NSString *fullPath = [dirPath stringByAppendingPathComponent:subPath];
        
        // 文件属性
        NSDictionary *attrs = [manager attributesOfItemAtPath:fullPath error:nil];
        
        // 注意3: 累加(attributes的fileSize属性直接返回文件的大小)
        size += attrs.fileSize;
//        size += [attrs[NSFileSize] unsignedLongLongValue];
        
    }
    
    // 4.返回总缓存大小
    return size;
}
  • 1.获得文件夹的一些属性资料(NSFileSize如果有子集目录,则文件大小不为NSFileSize,如果只有一个subPath才是该文件的大小,因为文件夹没有NSFileSize记录整个文件的大小,都是要通过遍历子目录计算的)

  • 2.注意:NSFilemanager的contentsOfDirectoryAtPath:和subpathsAtPath:方法的异同

  • 3.注意3: 累加(attributes的fileSize属性直接返回文件的大小)

  • 4.清除缓存操作要在子线程执行,计算完毕以后再回主线程刷新UI

  • 二.用遍历器保存所有子路径

- (unsigned long long)getSize{
    // 0.获得缓存文件路径
    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    ZGKLog(@"cachesPath: %@", cachesPath);
    NSString *dirPath = [cachesPath stringByAppendingPathComponent:@"default"];
    
    // 1.文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    
    // 2.获得文件夹得属性
    // 获得文件夹的一些属性资料(NSFileSize如果有子集目录,则文件大小不为NSFileSize,如果只有一个subPath才是该文件的大小,因为文件夹没有NSFileSize记录整个文件的大小,都是要通过遍历子目录计算的)
    NSDictionary *attrs = [manager attributesOfItemAtPath:dirPath error:nil];
    /*
     NSFileCreationDate = "2017-02-07 17:15:11 +0000";
     NSFileExtensionHidden = 0;
     NSFileGroupOwnerAccountID = 20;
     NSFileGroupOwnerAccountName = staff;
     NSFileModificationDate = "2017-02-09 17:56:09 +0000";
     NSFileOwnerAccountID = 501;
     NSFilePosixPermissions = 493;
     NSFileReferenceCount = 4;
     NSFileSize = 136;
     NSFileSystemFileNumber = 22754319;
     NSFileSystemNumber = 16777220;
     NSFileType = NSFileTypeDirectory;
     */
    ZGKLog(@"attrs :\n %@", attrs);
    
    // 获得文件夹一级目录
    NSArray *contentPaths = [manager contentsOfDirectoryAtPath:dirPath error:nil];
    NSLog(@"contents = %@", contentPaths);
    
//    // 2.获得文件夹所有子级目录
//    NSArray *subPaths = [manager subpathsAtPath:dirPath];
//    NSLog(@"subPaths = %@", subPaths);
    
    // 2.使用遍历器
    NSDirectoryEnumerator *enumerator = [manager enumeratorAtPath:dirPath];
    NSLog(@"enumerator = %@", enumerator);


    // 3.计算所有子级目录的大小(注意:在mac系统下1M = 1000KB,而不是1024)
    unsigned long long size = 0;
    for (NSString *subPath in enumerator) {
        // 全路径
        NSString *fullPath = [dirPath stringByAppendingPathComponent:subPath];
        
        // 文件属性
        NSDictionary *attrs = [manager attributesOfItemAtPath:fullPath error:nil];
        
        // 累加(fileSize直接返回文件的大小)
        size += attrs.fileSize;
//        size += [attrs[NSFileSize] unsignedLongLongValue];
        
    }
    
    // 4.返回总缓存大小
    return size; // 1168471
}

  • 注意使用的遍历器,用一个对象储存了所有的子路径
 // 2.使用遍历器
    NSDirectoryEnumerator *enumerator = [manager enumeratorAtPath:dirPath];
    NSLog(@"enumerator = %@", enumerator);

相关文章

  • 知识点总结28:计算缓存文件的大小

    计算缓存的方法:(用数组或者遍历器来保存子路径) 一.用数组保存所有子路径 1.获得文件夹的一些属性资料(NSFi...

  • 应用程序缓存

    计算单个文件大小 计算目录大小 清除缓存 SDWebImage框架自带请缓存方法

  • iOS清除缓存功能

    计算单个文件大小 计算目录大小 清除缓存文件 数据存储到iCloud

  • iOS 计算缓存大小

    1.计算Cache文件夹大小,包括了SDWebImage的图片缓存 2.计算数据库的大小 3.#清除缓存

  • iOS清除缓存

    #pragma mark - 第一步,计算缓存文件的大小 //首先获取缓存文件的路径 -(NSString *)g...

  • iOS拓展-手动清除缓存

    1. 计算缓存路径下的文件大小 2.清除缓存文件夹下的文件 3.清除触发事件

  • NSFileManager基本使用

    NSFileManager 文件操作在开发中用到的比较少,最常用的就是计算缓存的大小,删除缓存目录下的文件之类的 ...

  • Object-C_App缓存计算&删除

    Object-C 计算缓存大小 Swift计算缓存

  • iOS基础--一些常用的小知识(二)

    清理缓存(具体情况还需要具体调整) 计算缓存文件夹下缓存的大小. 代码延迟执行的方法 UIScrollView的常...

  • iOS-清除缓存功能(封装)

    应用场景: 现在众多app中都会有清楚缓存的功能,怎么能精确的计算缓存文件的大小,从而清除缓存文件呢,下面对清楚功...

网友评论

      本文标题:知识点总结28:计算缓存文件的大小

      本文链接:https://www.haomeiwen.com/subject/lxxgittx.html