首页 > 代码库 > 新浪微博客户端(61)-清除图片缓存

新浪微博客户端(61)-清除图片缓存

 

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清除缓存" style:UIBarButtonItemStylePlain target:self action:@selector(clearCache)];
    
 
    // 获取SDwebImage图片所占的磁盘容量大小(以byte为单位)
    NSUInteger byteSize = [SDImageCache sharedImageCache].getSize;
    double size = byteSize / 1000.0 / 1000.0; // mac和ios系统在byte和kb之前互转时,比是1000;
    
    self.navigationItem.title = [NSString stringWithFormat:@"缓存%.1fM",size];

}



// 清除缓存
- (void)clearCache {

    // 创建一个小菊花
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:indicator];
    [indicator startAnimating];
    
    // 清空SDWebImage缓存图片
    [[SDImageCache sharedImageCache] clearDisk];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清除缓存" style:UIBarButtonItemStylePlain target:self action:@selector(clearCache)];
    self.navigationItem.title = @"缓存0M";

}

最终效果:

技术分享

 

新浪微博客户端(61)-清除图片缓存