taiyang2014

iOS中清除缓存的方法 以及SDWebimage自带的清除缓存方法

1  SDWebimage中 

(1)  计算缓存的大小 

单位 : (MB)

  CGFloat size = [[SDImageCache sharedImageCache] getSize] / 1024 / 1024.;

(2)  清除缓存

给button设置一个点击事件, 弹出警告框

 UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"清理缓存" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

(3) 使用UIAlertView的回调方法判断点击的下标惊醒下一步操作

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [[SDImageCache sharedImageCache] clearDisk];
        [self.tableView reloadData];
}

 

/ /    自己实现清除缓存

- (void)cleanCase
{
    [self startAnimation];
    //创建文件夹
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //缓存
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    //判断是否存在一个文件或者文件夹
    BOOL flag = NO;
    BOOL result = [fileManager fileExistsAtPath:cachesPath isDirectory:&flag];
    if (result) {
        NSLog(@"存在");
        if (flag) {
            NSLog(@"是一个文件夹");
            NSError *error = nil;
            BOOL removeResult =[fileManager removeItemAtPath:cachesPath error:&error];
            if (removeResult) {
                NSLog(@"删除成功");
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"恭喜您" message:@"清除缓存成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
                [alertView release];
            }
        } else {
            NSLog(@"是一个文件");
        }
    } else {
        NSLog(@"不存在");
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"没有可清理的缓存了哦!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }

 

posted on 2015-03-19 19:46  taiyang2014  阅读(488)  评论(0编辑  收藏  举报

导航