ios清理缓存之封装清理缓存cell

这篇文章主要介绍方面的知识.主要为刚接触ios的小白提供思路,有误之处请大家多多指教--->>>新鲜出炉的.

1,清理缓存个人理解
有个很牛逼的加载图片的框架SDWebImage,大家应该都知道的.我也看过很多人写的清理缓存就是清理SDWebImage里面的缓存,那么如果我们系统里面有除了图片之外的加载了???SDWebImage就不可能帮助你完成你需要的计算了,因为框架内部"com.hackemist.SDWebImageCache"这个地址进行清理缓存.那么我们自己的网页缓存和视频缓存还是清理不了,所以清理缓存如果是用了SDWebImage框架必须清理两部分(自己的缓存和SDWebImage的缓存),若果不用框架就直接清理自己的缓存就行了.

2,先看看效果,我主要用的是系统的tableViewCell控件做的,大家可以根据自己的喜好加以修改:

ios清理缓存之封装清理缓存cell_第1张图片
计算清理缓存.png
ios清理缓存之封装清理缓存cell_第2张图片
缓存计算完成.png
ios清理缓存之封装清理缓存cell_第3张图片
缓存删除完成.png

3,封装获取缓存的文件的大小

    //总大小
    unsigned long long size = 0;
    
    //获取管理文件的对象
    NSFileManager *manager = [NSFileManager defaultManager];
    
    //判断是否是文件还是文件夹
    BOOL isDirectory = NO;
    
    //获取文件夹是否存在
    BOOL exists = [manager fileExistsAtPath:self isDirectory:&isDirectory];
    
    if (!exists) return size;
    
    if (isDirectory) {
        
        //获得文件的大下
        NSDirectoryEnumerator *enumerator = [manager enumeratorAtPath:self];
        
        //遍历文件遍历器
        for (NSString *subpath in enumerator) {
            
            //全路径
            NSString *fullPath = [self stringByAppendingPathComponent:subpath];
            
            //累加文件夹中所有文件的大小
            size += [manager attributesOfItemAtPath:fullPath error:nil].fileSize;
            
        }
    
    }else{
        
        //获取文件的大小
        size = [manager attributesOfItemAtPath:self error:nil].fileSize;
        
    }

4,封装清理缓存的cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        //设置cell右边的指示器(用来说明正在处理事情)
        UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        
        //开启菊花
        [indicatorView startAnimating];
        
        //设置cell的accessoryView
        self.accessoryView = indicatorView;
        
        //设置cell的默认显示文字
        self.textLabel.text = @"清除缓存(正在计算缓存大小...)";
        
        //取消用户点击监听
        self.userInteractionEnabled = NO;
        
        __weak typeof(self) weakself = self;
        
       //计算文件的打下
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
           
            //计算缓存文件的大小是耗时操作,最好放在子线程中执行
            unsigned long long size = @"/Users/bobo/Desktop/text".cacheFileSize;
            
            //SDImageCache的缓存大小
           //[SDImageCache sharedImageCache].getSize是SDImageImage框架获取它自己加载的缓存大小,有兴趣的同学可以进文件看下.
            size += [SDImageCache sharedImageCache].getSize;
            
            //如果cell已经销毁就直接返回
            if (weakself == nil) return ;
            
            //获取转换后的size大小
            //cacheTextForSize这个转换大家就去自己动手写写吧,用pow写很简单,不做详细介绍了
            NSString *cellText = [weakself cacheTextForSize:size];
            
/**
之所以用异步线程,因为计算文件大小是耗时操作文门应该放到子线程中,不能影响UI的显示.
*/
            //主线程做UI工作
            dispatch_async(dispatch_get_main_queue(), ^{
            
                //设置文字
                weakself.textLabel.text = cellText;
                
                //size计算完成后,设置accessoryView为空
                weakself.accessoryView = nil;
                
                //重新设置cell的右边为箭头状态
                weakself.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
           
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clearCache)];;
                [ weakself addGestureRecognizer:tap];
                
                //状态完成后,回复点击效果
                self.userInteractionEnabled = YES;
            });
            
        }); 
    }
    return self;
}

5,清理缓存


//清理缓存
- (void)clearCache{
    
    __weak typeof(self)weakself = self;

    //显示提醒
    [SVProgressHUD showWithStatus:@"正在清理缓存..."];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    //删除SDImage的缓存
  //clearDiskOnCompletion这里用这个完成Block主要是因为SDImageCache的clearDisk方法是异步子线程,我们的dispatch_get_global_queue也是异步子线程,这也可能造成主线程卡死.所以选在先让框架的删除,在做自己的事情.
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        
        //清除自定义路径下的文件内容
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
           
            NSFileManager *manager = [NSFileManager defaultManager];
            
            //移除缓存文件
            [manager removeItemAtPath:cacheFilePath(customFilePath) error:nil];
            
            //创建缓存文件
            [manager createDirectoryAtPath:cacheFilePath(customFilePath) withIntermediateDirectories:YES attributes:nil error:nil];
            
            dispatch_async(dispatch_get_main_queue(), ^{
               
                //移除提示
                [SVProgressHUD dismiss];

                //重新设置文字
                weakself.textLabel.text = @"清除缓存(0B)";
            });
        });
    }];
}

6,细节处理
在上面我们用到了UIActivityIndicatorView,那么问题来了.当我们把tableView上下滑动的时候的,cell的UIActivityIndicatorView会消失不见.这是问什么了?
这是因为动画效果导致的,解决办法就是在每次cell出现的时候拿到UIActivityIndicatorView,让它开始动画就行了.

 //cell每次出现的页面都会调用这个方法,取出这个indicatorView刷新UI界面
    UIActivityIndicatorView *indicatorView = (UIActivityIndicatorView *)self.accessoryView;
    [indicatorView startAnimating];

7,结束语
这就是我总结的清理缓存的知识,希望可以帮到大家.如有问题欢迎联系我,我会第一时间回答大家的问题.

你可能感兴趣的:(ios清理缓存之封装清理缓存cell)