iOS-计算文件/文件夹大小,删除文件,以及SDWebImage清除缓存

接着上篇下载word/pdf/txt等文档的文章,此篇涉及到清除这些文档。本文实现文件/文件夹大小的计算以及清除文件,还有清除SDWebImage缓存图片的一些内容。不废话,直接上代码。

一.计算文件/文件夹大小

/**
 获取文件/文件夹大小

 @param fileName 文件/文件夹路径
 @return 大小
 */
- (CGFloat)getFileSizeWithFileName:(NSString *)fileName {
     //文件管理者
     NSFileManager *mgr = [NSFileManager defaultManager];
     //判断字符串是否为文件/文件夹
     BOOL dir = NO;
     BOOL exists = [mgr fileExistsAtPath:fileName isDirectory:&dir];
    //文件/文件夹不存在
    if (exists == NO) return 0;
    //self是文件夹
    if (dir){
         //遍历文件夹中的所有内容
         NSArray *subpaths = [mgr subpathsAtPath:fileName];
         //计算文件夹大小
         NSInteger totalByteSize = 0;
         for (NSString *subpath in subpaths){
                  //拼接全路径
                   NSString *fullSubPath = [fileName stringByAppendingPathComponent:subpath];
               //判断是否为文件
                BOOL dir = NO;
                [mgr fileExistsAtPath:fullSubPath isDirectory:&dir];
                 if (dir == NO){//是文件
                    NSDictionary *attr = [mgr attributesOfItemAtPath:fullSubPath error:nil];
                    totalByteSize += [attr[NSFileSize] integerValue];
                }
            }
         return totalByteSize;
        
    } else{//是文件
        NSDictionary *attr = [mgr attributesOfItemAtPath:fileName error:nil];
        return [attr[NSFileSize] floatValue];
    }
}

二.获取文件夹下文件大小和SDWebImage缓存大小

CGFloat totalSize = 0;
    
    //获取Documents下文件的总共大小
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths lastObject];
    CGFloat docSize = [self getFileSizeWithFileName:documentsDirectory];
    NKLog(@"docSize=%ld",(long)docSize);
    
    //获取SDWebimage缓存的图片大小
    CGFloat imageSize = [[SDImageCache sharedImageCache] getSize];
    NKLog(@"imageSize=%lu",(unsigned long)imageSize);
    
    //缓存图片大小+下载的word文档大小
    totalSize = (docSize + imageSize)/1000/1000;
    
    self.memeryLabel.text = [NSString stringWithFormat:@"%.2fM",totalSize];

三.清除文件和缓存图片

/**
 清除缓存
 */
-(void)clearMemery {
    NSUInteger cacheSize = [[SDImageCache sharedImageCache] getSize];
    if (cacheSize == 0) {
        [MBProgressHUD showSuccess:@"没有缓存" toView:self.view];
    }else {
        [[SDImageCache sharedImageCache] clearMemory];
        [MBProgressHUD showMessage:@"正在清除缓存" toView:self.view];
        
        //清除Documents下的word文档
        NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:DocumentsPath];
        for (NSString *fileName in enumerator) {
            [[NSFileManager defaultManager] removeItemAtPath:[DocumentsPath stringByAppendingPathComponent:fileName] error:nil];
        }
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [MBProgressHUD showSuccess:@"清除缓存成功"];
            [MBProgressHUD hideHUDForView:self.view];
            self.memeryLabel.text = @"0.0M";
        });
    }
}

好了,实现了,慕言的世界!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前不久做了一个生成快照的需求,其中用到 SDWebImage 来下载图片,在使用该框架的过程中也遇到了一些问题,索...
    ShannonChenCHN阅读 14,774评论 12 241
  • App后期优化必须要考虑的问题——缓存。App基本功能——清除缓存。最近在做清除缓存的功能,所以好好研究了一下。这...
    墨痕未干阅读 6,298评论 0 14
  • 一个特别想成功的男子去请教长者,长者让他去河边,然后把他按在水里,过了很长时间,任凭他挣扎,最终终于他挣扎了出来。...
    橘子郡的天空阅读 1,657评论 0 0
  • 你是天上的云朵 我是地面的湖泊 你偶然从我的头顶飘过 倩影深深印在了我的心窝 从那以后 我便迷失了自我 幻想也是一...
    梦依寒烟醉阅读 2,981评论 0 0
  • 稿子还是没过,自从3月份以来,换成了新客户之后,就一直是这么个状态。坚持了很久,说你要加油,你要更努力,你要珍惜机...
    冬大至阅读 3,971评论 2 2