计算缓存文件大小、清除缓存的Cell
计算缓存文件大小
- (void)getCacheSize
{
// 总大小
unsigned long long size = 0; // 获得缓存文件夹路径
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSString *dirpath = [cachesPath stringByAppendingPathComponent:@"MP3"]; // 文件管理者
NSFileManager *mgr = [NSFileManager defaultManager]; // 获得文件夹的大小 == 获得文件夹中所有文件的总大小
// XMGLog(@"contents - %@", [mgr contentsOfDirectoryAtPath:dirpath error:nil]);
NSArray *subpaths = [mgr subpathsAtPath:dirpath];
for (NSString *subpath in subpaths) {
// 全路径
NSString *fullSubpath = [dirpath stringByAppendingPathComponent:subpath];
// 累加文件大小
size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize; // NSDictionary *attrs = [mgr attributesOfItemAtPath:fullSubpath error:nil];
// size += [attrs[NSFileSize] unsignedIntegerValue];
} NSLog(@"%zd", size);
}
计算缓存文件大小工具封装 (可给NSString增加分类)
//声明
- (unsigned long long)fileSize; //实现
- (unsigned long long)fileSize
{
// 总大小
unsigned long long size = 0; // 文件管理者
NSFileManager *mgr = [NSFileManager defaultManager]; // 是否为文件夹
BOOL isDirectory = NO; // 路径是否存在
BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
if (!exists) return size; if (isDirectory) { // 文件夹
// 获得文件夹的大小 == 获得文件夹中所有文件的总大小
NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
for (NSString *subpath in enumerator) {
// 全路径
NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
// 累加文件大小
size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
}
} else { // 文件
size = [mgr attributesOfItemAtPath:self error:nil].fileSize;
} return size;
}
清除缓存的Cell 自定义Cell 声明与实现
//
// JGClearCacheCell.h
//
//
// Created by JG on 16/12/08.
// Copyright © 2016年 JG. All rights reserved.
// #import <UIKit/UIKit.h> @interface JGClearCacheCell : UITableViewCell @end //
// JGClearCacheCell.m
//
//
// Created by JG on 16/12/08.
// Copyright © 2016年 JG. All rights reserved.
// #import "JGClearCacheCell.h"
#import <SDImageCache.h>
#import <SVProgressHUD.h> #define JGCustomCacheFile [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Custom"] @implementation JGClearCacheCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// 设置cell右边的指示器(用来说明正在处理一些事情)
UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[loadingView startAnimating];
self.accessoryView = loadingView; // 设置cell默认的文字(如果设置文字之前userInteractionEnabled=NO, 那么文字会自动变成浅灰色)
self.textLabel.text = @"清除缓存(正在计算缓存大小...)"; // 禁止点击
self.userInteractionEnabled = NO; // int age = 10;
// typeof(age) age2 = 10; // __weak JGClearCacheCell * weakSelf = self;
__weak typeof(self) weakSelf = self; // 在子线程计算缓存大小
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:2.0]; // 获得缓存文件夹路径
unsigned long long size = JGCustomCacheFile.fileSize;
size += [SDImageCache sharedImageCache].getSize; // 如果cell已经销毁了, 就直接返回
if (weakSelf == nil) return; NSString *sizeText = nil;
if (size >= pow(10, 9)) { // size >= 1GB
sizeText = [NSString stringWithFormat:@"%.2fGB", size / pow(10, 9)];
} else if (size >= pow(10, 6)) { // 1GB > size >= 1MB
sizeText = [NSString stringWithFormat:@"%.2fMB", size / pow(10, 6)];
} else if (size >= pow(10, 3)) { // 1MB > size >= 1KB
sizeText = [NSString stringWithFormat:@"%.2fKB", size / pow(10, 3)];
} else { // 1KB > size
sizeText = [NSString stringWithFormat:@"%zdB", size];
} // 生成文字
NSString *text = [NSString stringWithFormat:@"清除缓存(%@)", sizeText]; // 回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 设置文字
weakSelf.textLabel.text = text;
// 清空右边的指示器
weakSelf.accessoryView = nil;
// 显示右边的箭头
weakSelf.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 添加手势监听器
[weakSelf addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:weakSelf action:@selector(clearCache)]]; // 恢复点击事件
weakSelf.userInteractionEnabled = YES;
});
});
}
return self;
} /**
* 清除缓存
*/
- (void)clearCache
{
// 弹出指示器
[SVProgressHUD showWithStatus:@"正在清除缓存..." maskType:SVProgressHUDMaskTypeBlack]; // 删除SDWebImage的缓存
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
// 删除自定义的缓存
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr removeItemAtPath:JGCustomCacheFile error:nil];
[mgr createDirectoryAtPath:JGCustomCacheFile withIntermediateDirectories:YES attributes:nil error:nil]; // 所有的缓存都清除完毕
dispatch_async(dispatch_get_main_queue(), ^{
// 隐藏指示器
[SVProgressHUD dismiss]; // 设置文字
self.textLabel.text = @"清除缓存(0B)";
});
});
}];
} /**
* 当cell重新显示到屏幕上时, 也会调用一次layoutSubviews
*/
- (void)layoutSubviews
{
[super layoutSubviews]; // cell重新显示的时候, 继续转圈圈
UIActivityIndicatorView *loadingView = (UIActivityIndicatorView *)self.accessoryView;
[loadingView startAnimating];
} @end
计算缓存文件大小、清除缓存的Cell的更多相关文章
- SP 页面缓存以及清除缓存
JSP 页面缓存以及清除缓存 一.概述 缓存的思想可以应用在软件分层的各个层面.它是一种内部机制,对外界而言,是不可感知的. 数据库本身有缓存,持久层也可以缓存.(比如:hibernate,还分1级和 ...
- APICloud 获取缓存以及清除缓存(常用第三方方法)
一.app中经常会有缓存的清除这个操作,具体如下 1.获取缓存大小 apiready = function() { api.getCacheSize(function(ret, err) { //si ...
- CodeIgniter启用缓存和清除缓存的方法
Codeigniter支持缓存技术,以达到最快的速度.尽管CI已经相当高效了,但是网页中的动态内容.主机的内存CPU和数据库读取速度等因素直接影响了网页的加载速度.依靠网页缓存,你的网页可以达到近乎静 ...
- JSP 页面缓存以及清除缓存
一.概述 缓存的思想可以应用在软件分层的各个层面.它是一种内部机制,对外界而言,是不可感知的. 数据库本身有缓存,持久层也可以缓存.(比如:hibernate,还分1级和2级缓存) 业务层也可以有缓存 ...
- SDWebImage实现图片展示、缓存、清除缓存
1. /* 图片显示 */ [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlString]]; [s ...
- iOS开发 -李洪强-清除缓存
// // SetViewController.m // dfhx // // Created by dfhx_iMac_001 on 16/4/5. // Copyright © 2016年 ...
- android 清除缓存功能
本应用数据清除管理器 DataCleanManager.java 是从网上摘的 忘了 名字了 对不住了 载入一个webview 产生缓存 众所周知的webview是产生缓存的主要原因之中的一 ...
- 计算app内部缓存文件大小
#pragma mark - 计算单个文件大小 - (long long)fileSizeAtPath:(NSString*)filePath{ NSFileManager* manager = [N ...
- (一一七)基本文件操作 -SDWebImage清除缓存 -文件夹的大小计算
在iOS的App沙盒中,Documents和Library/Preferences都会被备份到iCloud,因此只适合放置一些记录文件,例如plist.数据库文件.缓存一般放置到Library/Cac ...
随机推荐
- ueditor编辑器使用
下载ueditor1_4_3_3-gbk-asp 解压后重命名为ueditor上传至网站 在需要编辑器的位置增加asp代码: <td style="PADDING-LEFT: 10px ...
- JS学习总结(新手)
1. JS面向对象 http://www.cnblogs.com/JavascriptDream/p/5064976.html a. Prototype 属性的理解 b. 遗传继承函数 functio ...
- Mybatis缓存处理机制
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- div span
无牵无挂,不带任何样式,因此经常使用div完成整体样式的构建,span完成细微样式的构建. div为块级元素,span为行内元素. 使用div完成显示区域的居中.左右浮动等,完成整体的样式布局,然后在 ...
- js 基础
js基础知识点总结 如何在一个网站或者一个页面,去书写你的js代码:1.js的分层(功能):jquery(tool) 组件(ui) 应用(app),mvc(backboneJs)2.js的规划():避 ...
- ab post 测试 http 和 webservice 接口方法及用例
1.ab测试简单http请求 ab -n30000 -c1000 "http://10.1.1.21:8080/" 2.ab 测试 http 接口 (POST) ab -n400 ...
- ECMAScript 5中的数据属性和访问器属性
简介 ECMAScript 定义的对象中有两种特殊的属性, 这两种特殊的属性在你定义对象属性时就会赋予, 我们在必要时可以改写这两种特殊的属性让其属性的访问更加的合理化, 这两种特殊的属性称呼及作用如 ...
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 // 节中包括 System.Web.SessionSta
我直接在父类的构造方法中调用了sessionj结果就报这个错误 搜了好久 让改web.config 可是不起作用 代码如下: public class BasePage:System.Web.UI.P ...
- RDIFramework.NET-.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(MVC版)
RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(MVC版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之产品管理 ...
- winserver2008 DNS 很详细
from http://www.it165.net/admin/html/201312/2182.html DNS(Domain Name System域名系统)区域化管理 分布式 层次性 域名空间结 ...