ios - 清理缓存

作者: flowerflower | 来源:发表于2017-07-24 17:18 被阅读1335次

    直接上demo

    LYFielService.h

    
    @interface LYFielService : NSObject
    //计算单个文件大小
    +(float)fileSizeAtPath:(NSString *)path;
    //清理缓存文件
    +(void)clearCache:(NSString *)path;
    //计算目录大小
    +(float)folderSizeAtPath:(NSString *)path;
    //缓存路径
    +(NSString *)getPath;
    @end
    

    LYFielService.m

    #import "LYFielService.h"
    #import <SDWebImage/SDImageCache.h>
    @implementation LYFielService
    
    //计算单个文件大小
    +(float)fileSizeAtPath:(NSString *)path{
        NSFileManager *fileManager=[NSFileManager defaultManager];
        if([fileManager fileExistsAtPath:path]){
            long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
            return size/1024.0/1024.0;
        }
        return 0;
    }
    
    //清理缓存文件
    +(void)clearCache:(NSString *)path{
        NSFileManager *fileManager=[NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:path]) {
            NSArray *childerFiles=[fileManager subpathsAtPath:path];
            for (NSString *fileName in childerFiles) {
                //如有需要,加入条件,过滤掉不想删除的文件
                NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
                [fileManager removeItemAtPath:absolutePath error:nil];
            }
        }
    //     [[SDImageCache sharedImageCache] clearDisk]; //关键是这句 4.0之后没有该方法 则直接使用下面的方法即可
        [[SDImageCache sharedImageCache]clearDiskOnCompletion:nil];
    }
    //计算目录大小
    +(float)folderSizeAtPath:(NSString *)path{
        NSFileManager *fileManager=[NSFileManager defaultManager];
        float folderSize;
        if ([fileManager fileExistsAtPath:path]) {
            NSArray *childerFiles=[fileManager subpathsAtPath:path];
            for (NSString *fileName in childerFiles) {
                NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
                folderSize +=[LYFielService fileSizeAtPath:absolutePath];
            }
            //SDWebImage框架自身计算缓存的实现
        folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        return folderSize;
        }
        return 0;
    }
    //缓存路径
    +(NSString *)getPath {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *path = [paths lastObject];
        return path;
    }
    @end
    

    在Controller的应用

    #pragma mark - Life Cycle
    - (void)viewWillAppear:(BOOL)animated{
         [super viewWillAppear:YES];
          [self.tableView reloadData];
    }
    #pragma mark - <UITableViewDataSource,UITableViewDelegate>
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCellID];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SettingCellID];        ;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
      cell.detailTextLabel.text = [self getCacheSize];
    return cell;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       [MBProgressHUD  LY_ShowSuccess:LYLocalizedString(@"正在清除缓存")  toView:self.view];
          [LYFielService clearCache:@"Library/Caches"];
        [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:1 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
    }
    #pragma mark - Custom Methods
    - (NSString *)getCacheSize{
        
      float size_m = [LYFielService folderSizeAtPath:@"Library/Caches"];
        return [NSString stringWithFormat:@"%.2fMB",size_m];
    
    }
    

    相关文章

      网友评论

        本文标题:ios - 清理缓存

        本文链接:https://www.haomeiwen.com/subject/stwbkxtx.html