iOS SDWebImage框架自身计算、清除缓存的实现

1、计算缓存的方法(path,缓存的路径)

//计算缓存

-(CGFloat)folderSizeAtPath:(NSString *)path


{


    CGFloat folderSize = 0;


    //文件管理对象



    NSFileManager *manager = [NSFileManager defaultManager];



    if ([manager fileExistsAtPath:path])


    {


        //SDWebImage框架自身计算缓存的实现



        folderSize += [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;


        return folderSize;


    }


    


    return 0;


}

2、清除缓存的方法(path,缓存的路径)

//清理缓存


-(void)cleanCache:(NSString *)path


{
    //获取文件管理对象


    NSFileManager *manager = [NSFileManager defaultManager];



    if ([manager fileExistsAtPath:path])


    {


        //获取目录下面的文件名字


        NSArray *childFiles = [manager subpathsAtPath:path];


        for (NSString *fileName in childFiles)


        {


            //拼接地址和文件名



            NSString *filePath = [NSString stringWithFormat:@"%@/%@",path,fileName];


            //清理文件



            NSError *error = nil;


            [manager removeItemAtPath:filePath error:&error];

        }


        //清理缓存



        [[SDImageCache sharedImageCache] cleanDisk];



        [SVProgressHUD showSuccessWithStatus:@"完成清理"];



    }

}

3、实现以上两个方法

- (void)viewDidLoad {
    [super viewDidLoad];

    //library  清除缓存的路径
    NSString *path1 =  NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
    //计算缓存
    CGFloat folder = [self folderSizeAtPath:path1];
  NSString *str = [NSString stringWithFormat:@"有%.2fM的缓存,确定要清理缓存吗?",folder];
NSLog(@"%@",str);

    //清理缓存
    //[self cleanCache:path];
    [self cleanCache:path1];

你可能感兴趣的:(iOS SDWebImage框架自身计算、清除缓存的实现)