iOS沙盒缓存的路径获取,大小计算,删除

1.常规文件获取删除

2.SDWebImage获取删除

1.1沙盒路径获取

    //NSDocumentDirectory、NSCachesDirectory、NSLibraryDirectory 沙盒的document、caches、library 

    NSArray *pathss = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [pathss objectAtIndex:0];
    
    NSString *imgCachePath = [NSString stringWithFormat:@"%@/Caches/default/com.hackemist.SDWebImageCache.default",documentsDirectory];
    
    if ([fileManager fileExistsAtPath:imgCachePath]) {
        
               NSLog(@"用户文件已存在");
        
       NSString *STR = [self getCacheSizeWithFilePath:imgCachePath];
       deleteCachesImgValue.text = STR;

    }else {
               NSLog(@"用户文件不存在");

        deleteCachesImgValue.text = @"0 K";
        
    }
    

1.2缓存大小计算

- (NSString *)getCacheSizeWithFilePath:(NSString *)path{
    
    //调试
#ifdef DEBUG
    //如果文件夹不存在或者不是一个文件夹那么就抛出一个异常
    //抛出异常会导致程序闪退,所以只在调试阶段抛出,发布阶段不要再抛了,不然极度影响用户体验
    BOOL isDirectory = NO;
    BOOL isExist = [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    if (!isExist || !isDirectory)
    {
        NSException *exception = [NSException exceptionWithName:@"fileError" reason:@"please check your filePath!" userInfo:nil];
        [exception raise];
        
    }
//    NSLog(@"debug");
    //发布
#else
//    NSLog(@"post");
#endif
    
    //获取“path”文件夹下面的所有文件
    NSArray *subpathArray= [fileManager subpathsAtPath:path];
    
    NSString *filePath = nil;
    NSInteger totleSize=0;
    
    for (NSString *subpath in subpathArray)
    {
        //拼接每一个文件的全路径
        filePath =[path stringByAppendingPathComponent:subpath];
                
        //isDirectory,是否是文件夹,默认不是
        BOOL isDirectory = NO;
        
        //isExist,判断文件是否存在
        BOOL isExist = [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
        
        if (!isExist || isDirectory || [filePath containsString:@".DS"]) continue;

        NSDictionary *dict=   [fileManager attributesOfItemAtPath:filePath error:nil];
        
        NSInteger size=[dict[@"NSFileSize"] integerValue];
        totleSize+=size;
    }
    
    //将文件夹大小转换为 M/KB/B
    NSString *totleStr = nil;
    
    if (totleSize > 1000 * 1000)
    {
        totleStr = [NSString stringWithFormat:@"%.1fM",totleSize / 1000.0f /1000.0f];
    }else if (totleSize > 1000)
    {
        totleStr = [NSString stringWithFormat:@"%.1fKB",totleSize / 1000.0f ];
        
    }else
    {
        totleStr = [NSString stringWithFormat:@"%.1fB",totleSize / 1.0f];
    }
    
    return totleStr;
    
}

1.3缓存清理

//删除图片缓存方法
- (void)deleteimages{
    
    NSArray *pathss = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [pathss objectAtIndex:0];
    
    NSString *imgCachePath = [NSString stringWithFormat:@"%@/Caches/default/com.hackemist.SDWebImageCache.default",documentsDirectory];
    
    
    BOOL res=[fileManager removeItemAtPath:imgCachePath error:nil];
    
    if (res) {
        
                NSLog(@"文件删除成功");

    }else{
                NSLog(@"文件删除失败");
    }
 
}

2.1SD获取、计算

        NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
        NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];

2.2SD清理

    [[[SDWebImageManager sharedManager] imageCache] clearMemory]; //内存
    [[[SDWebImageManager sharedManager] imageCache] clearDisk]; //磁盘

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

推荐阅读更多精彩内容