iOS 用 SDWebImage 清理图片缓存
效果图如下:

1.找到 SDWebImage找到SDImageCache类
2.添加如下方法
- (float)checkTmpSize
{
float totalSize = ;
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 ;
} - (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==) {
self.lbl.frame= CGRectMake(,,, 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 ;
} #pragma Mark - 选中跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==) {
[self alertView]; }else if (indexPath.row==){
EvaluationViewController *evaluation=[EvaluationViewController new];
[self.navigationController pushViewController:evaluation animated:YES];
}
else if (indexPath.row==){
BusinessViewController *business=[BusinessViewController new];
[self.navigationController pushViewController:business animated:YES]; }else if (indexPath.row==){
VersionViewController *version=[VersionViewController new];
[self.navigationController pushViewController:version animated:YES]; }else if (indexPath.row==){
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
iOS 用 SDWebImage 清理图片缓存的更多相关文章
- SDWebImage清理图片缓存方法
//获取当前缓存大小 @property (nonatomic, assign) NSUInteger tmpSize; //获取缓存大小并储存 _tmpSize=[[SDImageCache sha ...
- SDWebImage实现图片缓存
我之前写过一篇博客,介绍缓存处理的三种方式,其中最难,最麻烦,最占内存资源的还是图片缓存,最近做的项目有大量的图片处理,还是采用了SDWebImage来处理,但是发现之前封装好的代码报错了.研究发现, ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
- Fast Image Cache – iOS 应用程序高性能图片缓存
Fast Image Cache 是一种在 iOS 应用程序中高效.持续.超快速的存储和检索图像的解决方案.任何良好的 iOS 应用程序的用户体验都应该是快速,平滑滚动的,Fast Image Cac ...
- iOS网络加载图片缓存与SDWebImage
加载网络图片可以说是网络应用中必备的.如果单纯的去下载图片,而不去做多线程.缓存等技术去优化,加载图片时的效果与用户体验就会很差. 一.自己实现加载图片的方法 tips: *iOS中所有网络访问都是异 ...
- iOS之SDWebImage清理缓存
.找到 SDWebImage找到SDImageCache类 添加如下方法 - (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumera ...
- SDWebImage清除图片缓存
背景: 使用 SDWebImage 库,由于内存中一直缓存着加载的图片,而导致内存过高(我们无法手动管理内存),弹出内存警告而导致程序很卡或者直接crash掉. 我的解决方法: 在AppDelegat ...
- iOS中 SDWebImage手动清除缓存的方法 技术分享
1.找到SDImageCache类 2.添加如下方法: - (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumerator *file ...
- iOS利用SDWebImage实现缓存的计算与清理
概述 可以仅仅清理图片缓存, 也可以清理所有的缓存文件(包括图片.视频.音频等). 详细 代码下载:http://www.demodashi.com/demo/10717.html 一般我们项目中的缓 ...
随机推荐
- KVC & KVO
KVC和KVO看上去又是两个挺牛的单词简写,KVC是Key-Value Coding的简写,是键值编码的意思.KVO是Key-Value Observing的简写,是键值观察的意思.那么我们能拿KV ...
- ModelState.IsValid总为false原因
总结在开发中遇到的一个问题 ModelState.IsValid 一直是false 且在局部变量中,没有发现有问题啊,Model非常正常有木有,可是为什么 ModelState.IsValid 总是f ...
- How to implement a neural network
神经网络的实践笔记 link: http://peterroelants.github.io/posts/neural_network_implementation_part01/ 1. 生成训练数据 ...
- Android之探究viewGroup自定义子属性参数的获取流程
通常会疑惑,当使用不同的布局方式时,子view得布局属性就不太一样,比如当父布局是LinearLayout时,子view就能有效的使用它的一些布局属性如layout_weight.weightSum. ...
- IOS 整体架构 和 MVC布局
IOS的生态系统 IOS生态系统不仅仅是指产品,更重要的是指 iPhone/iPad/iPod/Mac +iCloud+App整个系统,包括Siri (部分设备不支持).FaceTime.Safari ...
- (2)从实际项目谈起,基于MEF的插件框架之总体设计
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.MEF框架简介 MEF的全称是Managed Extensibil ...
- 【集合框架】Java集合框架综述
一.前言 现笔者打算做关于Java集合框架的教程,具体是打算分析Java源码,因为平时在写程序的过程中用Java集合特别频繁,但是对于里面一些具体的原理还没有进行很好的梳理,所以拟从源码的角度去熟悉梳 ...
- 详解用Navicat工具将Excel中的数据导入Mysql中
第一步:首先需要准备好有数据的excel: 第二步:选择"文件"->"另存为",保存为"CSV(逗号分隔)(*.csv)",将exce ...
- TCP三次握手和四次挥手状态变迁解析
TCP是TCP/IP的传输层控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 首先需要了解几个名词:tcp标志位,有6种分别为:SYN(synchronous建立联机) .ACK(ackn ...
- Matches正则使用提取内容
用VS新建WinForm程序,窗体上是三个文本框和一个按钮.可以自己构造正则表达式,自己修改匹配内容 正则表达是要提取的部分为hewenqitext 代码如下: using System; using ...