• 文件缓存的实现


     

    #import "LZJSettingViewController.h"

    #import <SDImageCache.h>

    #import "LZJFileTool.h"

    #import <SVProgressHUD/SVProgressHUD.h>

    #define CachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

     

    @interface LZJSettingViewController ()

    @property (nonatomic, assign) NSInteger totalSize;

    @end

     

    @implementation LZJSettingViewController

     

    static NSString * const ID = @"cell";

     

    - (void)viewDidLoad {

        

        [super viewDidLoad];

      

        // 设置导航条左边按钮

        self.title = @"设置";

        

        // 设置右边

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"jump" style:0 target:self action:@selector(jump)];

        

        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];

        

        [SVProgressHUD showWithStatus:@"正在计算缓存尺寸...."];

        

        // 获取文件夹尺寸

        // 文件夹非常小,如果我的文件非常大

        [LZJFileTool getFileSize:CachePath completion:^(NSInteger totalSize) {

            

            _totalSize = totalSize;

            

            [self.tableView reloadData];

            

            [SVProgressHUD dismiss];

        }];

    }

     

    - (void)jump

    {

        UIViewController *vc = [[UIViewController alloc] init];

        vc.view.backgroundColor = [UIColor redColor];

        [self.navigationController pushViewController:vc animated:YES];

    }

     

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return 1;

    }

     

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

        

        // 计算缓存数据,计算整个应用程序缓存数据 => 沙盒(Cache) => 获取cache文件夹尺寸

        

        // 获取缓存尺寸字符串

        cell.textLabel.text = [self sizeStr];

        

        return cell;

    }

     

    // 点击cell就会调用

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        // 清空缓存

        // 删除文件夹里面所有文件

        [LZJFileTool removeDirectoryPath:CachePath];

        

        _totalSize = 0;

        

        [self.tableView reloadData];

    }

     

     // 获取缓存尺寸字符串

    - (NSString *)sizeStr

    {

        NSInteger totalSize = _totalSize;

        NSString *sizeStr = @"清除缓存";

        // MB KB B

        if (totalSize > 1000 * 1000) {

            // MB

            CGFloat sizeF = totalSize / 1000.0 / 1000.0;

            sizeStr = [NSString stringWithFormat:@"%@(%.1fMB)",sizeStr,sizeF];

        } else if (totalSize > 1000) {

            // KB

            CGFloat sizeF = totalSize / 1000.0;

            sizeStr = [NSString stringWithFormat:@"%@(%.1fKB)",sizeStr,sizeF];

        } else if (totalSize > 0) {

            // B

            sizeStr = [NSString stringWithFormat:@"%@(%.ldB)",sizeStr,totalSize];

        }

     

        return sizeStr;

    }

     

    @end

  • 相关阅读:
    sosex !mk
    使用boost iostream写文件
    编译ios版本的ffmpeg0.11.1
    转换日期为中文数字
    sosex !muf查看方法的反汇编代码
    Xcode 4.5.2 + iOS 6.0免证书(iDP)开发+真机调试+生成IPA全攻略
    sosex查看变量的命令!mdt
    vs2012编译openssl
    为ios编译openssl
    打开的页面关闭后刷新主页面
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5501969.html
Copyright © 2020-2023  润新知