计算缓存文件大小、清除缓存的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 ...
随机推荐
- 优惠分摊算法 php版
<?php /* * 优惠分摊,算法很多,这里是从shopnc挖出来,适合优惠条件过滤的算法,实质很简单,但是理解难度还是有一点 * * 一个订单的商品,如果不参与某种活动,需要分摊优惠,一般来 ...
- Windows自带压缩解压工具
压缩一个文件: 命令行:makecab fileName.txt fileName.zip 鼠标操作:选中文件-->鼠标右键-->Send to-->Compressed (zipp ...
- MySQL:动态开启慢查询日志(Slow Query Log)
前言 在开发中,高效能的程序 也包括 高效能的查询,所以优化SQL也是程序员必要技能之一.要优化就必须要有慢日志记录才可以知道哪些查询慢,然后反向去修改 慢日志设置方式 写入文件 写入数据库 实践操作 ...
- Git和Github简单教程(转)
这篇文章只挑一部分命令来讲,差不多够用就行的程度.如果需要其他命令,到时候再去其他地方了解就行了先在Github上写好再搬过来的:本文Github链接 目录: 零.Git是什么 一.Git的主要功能: ...
- Solr Python API : SolrCloudpy 与 Pysolr 的 对比
http://ae.yyuap.com/pages/viewpage.action?pageId=920314 SolrCloudpy文档:http://solrcloudpy.github.io/s ...
- 关于 windows 下 node_modules\node-sass\vendor 的报错解决方法
项目git clone下来之后,运行npminstall, npm start报错代码如下: ERROR in ENOENT: no such file or directory, scandir ' ...
- iOS应用程序的生命周期
iOS应用程序一般都是由自己编写的代码和系统框架(system frameworks)组成,系统框架提供一些基本infrastructure给所有app来运行,而你提供自己编写的代码来定制app的外观 ...
- 关于sed用法
sed处理流程 sed的处理流程,简化后是这样的: 1.读入新的一行内容到缓存空间: 2.从指定的操作指令中取出第一条指令,判断是否匹配pattern: 3.如果不匹配,则忽略后续的编辑命令,回到第2 ...
- php构造方法与析构方法
1. 构造方法 <?php //构造方法 __construct() 是在实例化对象时被自动调用 //用途:可以用于初始化程序(可以给成员属性赋值,也可以调用成员方法) //语法:[修饰符] f ...
- spdk intel
前言 继为SDN和NFV领域带来福音的DPDK之后,英特尔于2015年9月开始,逐步将为NVMe等新一代存储规范优化的Linux性能工具包SPDK(Storage Performance Develo ...