iOS 用 SDWebImage 清理图片缓存
效果图如下:
1.找到 SDWebImage找到SDImageCache类
2.添加如下方法
- (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:_diskCachePath]; for (NSString *fileName in fileEnumerator) { NSString *filePath = [_diskCachePath stringByAppendingPathComponent:fileName]; NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; unsigned long long length = [attrs fileSize]; totalSize += length / 1024.0 / 1024.0; } // NSLog(@"tmp size is %.2f",totalSize); return totalSize; }
具体用tableView实现
MoreViewController.h
#import "BaseViewController.h" #import "More.h" @interface MoreViewController : BaseViewController<UITableViewDataSource,UITableViewDelegate> @property(strong,nonatomic)UITableView *MoreTableView; @property(strong,nonatomic)NSArray *arrayTitle; @property(strong,nonatomic)NSArray *arrayImage; @end
MoreViewController.m
#import "MoreViewController.h" @interface MoreViewController () @property(strong,nonatomic)UILabel *lbl; @end @implementation MoreViewController - (void)viewDidLoad { [super viewDidLoad]; self.title=@"更多"; [self _loadData]; NSLog(@"%@",NSHomeDirectory()); } -(void)_loadData { // 设置tableview的title和image self.arrayTitle=@[@"清除缓存",@"给个评价",@"商务合作",@"检测行版本",@"欢迎页",@"关于"]; self.arrayImage=@[@"moreClear@2x",@"moreScore@2x",@"moreBusiness@2x",@"moreVersion@2x",@"moreWelcome@2x",@"moreAbout@2x"]; // 初始化UItableView self.MoreTableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; // 设置UItableView的背景色 self.MoreTableView.backgroundColor=[UIColor blackColor]; // 指定代理 self.MoreTableView.delegate=self; self.MoreTableView.dataSource=self; // 分割线的样式 self.MoreTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine; [self.view addSubview:self.MoreTableView]; // 指定唯一标识符 [self.MoreTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MoreTableCell"]; self.lbl=[[UILabel alloc]init]; self.lbl.textColor=[UIColor whiteColor]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arrayTitle.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreTableCell" forIndexPath:indexPath]; if (indexPath.row==0) { self.lbl.frame= CGRectMake(200,20,200, cell.frame.size.height*0.5); self.lbl.text=[NSString stringWithFormat:@"缓存为%.2fMB",[[SDImageCache sharedImageCache] checkTmpSize]]; [cell addSubview:self.lbl]; } // 设置Cell的背景色 cell.backgroundColor=[UIColor blackColor]; // 设置title的文字颜色 cell.textLabel.textColor=[UIColor whiteColor]; cell.textLabel.text=self.arrayTitle[indexPath.row]; cell.imageView.image=[UIImage imageNamed:self.arrayImage[indexPath.row]]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; } #pragma Mark - 选中跳转 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row==0) { [self alertView]; }else if (indexPath.row==1){ EvaluationViewController *evaluation=[EvaluationViewController new]; [self.navigationController pushViewController:evaluation animated:YES]; } else if (indexPath.row==2){ BusinessViewController *business=[BusinessViewController new]; [self.navigationController pushViewController:business animated:YES]; }else if (indexPath.row==3){ VersionViewController *version=[VersionViewController new]; [self.navigationController pushViewController:version animated:YES]; }else if (indexPath.row==4){ WelcomeViewController *welcome=[WelcomeViewController new]; [self.navigationController pushViewController:welcome animated:YES]; }else{ AboutViewController *about=[AboutViewController new]; [self.navigationController pushViewController:about animated:YES]; } } #pragma Mark - 弹出框 -(void)alertView { NSLog(@"-----"); float tmpSize=[[SDImageCache sharedImageCache] checkTmpSize]; UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 清除磁盘 [[SDImageCache sharedImageCache] clearDisk]; // 刷新数据 [self.MoreTableView reloadData]; }]; UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:ok]; [alert addAction:cancle]; [self presentViewController:alert animated:YES completion:nil]; } #pragma Mark - 整体刷新 -(void)viewWillAppear:(BOOL)animated { // [super viewWillAppear:YES]; [self.MoreTableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end