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 一般我们项目中的缓 ...
随机推荐
- IOS各种集合遍历效率对比
前言: 对于ios项目开发中总会遇见各种集合遍历,出于对各种遍历效率的好奇心,所以准备写个测试程序测试一下 首先:先声明一个NSMutableArray,测试数据量分别是1000条,10000条,10 ...
- Util应用程序框架公共操作类(八):Lambda表达式公共操作类(二)
前面介绍了查询的基础扩展,下面准备给大家介绍一些有用的查询封装手法,比如对日期范围查询,数值范围查询的封装等,为了支持这些功能,需要增强公共操作类. Lambda表达式公共操作类,我在前面已经简单介绍 ...
- etlpy: 并行爬虫和数据清洗工具(开源)
etlpy是python编写的网页数据抓取和清洗工具,核心文件etl.py不超过500行,具备如下特点 爬虫和清洗逻辑基于xml定义,不需手工编写 基于python生成器,流式处理,对内存无要求 内置 ...
- angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”
曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...
- MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误
前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...
- weight属性你用的真的6嘛?
相信大家在日常开发中一定使用过weight这个属性,它的作用一个是权重,另一个就是渲染优先级,但是你真的能很6的使用它嘛?如果不是,那么请继续往下看!!! 我们知道,当weight起到不同作用的时候, ...
- TP框架执行流程分许
1. index.php 入口文件 2. ThinkPHP/ThinkPHP.php 在php5.3版本以后 设置常量有两种方式: const name = value; 作用域根据当前命名空间决定 ...
- 实现一个纵向排列的 ListBox ,并具有操作按钮
需要实现的效果如下: 要想把 ListBox 的内容纵向显示很简单,只需把 ListBox 的内容控件为 WrapPanel 就可以了: <ListBox.ItemsPanel> < ...
- 【转】单例模式(Singleton)
首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个, 这里就可以通过单例模式来避免两个打印作业同时输 ...
- [WCF编程]8.服务实例的生命周期
一.服务实例的生命周期概览 我们已经直到,通过显式调用Close方法或等待默认的超时时间到来,都可以释放服务实例.但是,在会话连接里,经常需要按一定顺序调用方法. 二.分步操作 会话契约的操作有时隐含 ...