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";
        });
    }
}

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

你可能感兴趣的:(iOS-计算文件/文件夹大小,删除文件,以及SDWebImage清除缓存)