计算缓存文件大小

- (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的更多相关文章

  1. SP 页面缓存以及清除缓存

    JSP 页面缓存以及清除缓存 一.概述 缓存的思想可以应用在软件分层的各个层面.它是一种内部机制,对外界而言,是不可感知的. 数据库本身有缓存,持久层也可以缓存.(比如:hibernate,还分1级和 ...

  2. APICloud 获取缓存以及清除缓存(常用第三方方法)

    一.app中经常会有缓存的清除这个操作,具体如下 1.获取缓存大小 apiready = function() { api.getCacheSize(function(ret, err) { //si ...

  3. CodeIgniter启用缓存和清除缓存的方法

    Codeigniter支持缓存技术,以达到最快的速度.尽管CI已经相当高效了,但是网页中的动态内容.主机的内存CPU和数据库读取速度等因素直接影响了网页的加载速度.依靠网页缓存,你的网页可以达到近乎静 ...

  4. JSP 页面缓存以及清除缓存

    一.概述 缓存的思想可以应用在软件分层的各个层面.它是一种内部机制,对外界而言,是不可感知的. 数据库本身有缓存,持久层也可以缓存.(比如:hibernate,还分1级和2级缓存) 业务层也可以有缓存 ...

  5. SDWebImage实现图片展示、缓存、清除缓存

    1. /* 图片显示 */ [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlString]];                [s ...

  6. iOS开发 -李洪强-清除缓存

    // //  SetViewController.m //  dfhx // //  Created by dfhx_iMac_001 on 16/4/5. //  Copyright © 2016年 ...

  7. android 清除缓存功能

    本应用数据清除管理器 DataCleanManager.java   是从网上摘的 忘了 名字了 对不住了 载入一个webview   产生缓存  众所周知的webview是产生缓存的主要原因之中的一 ...

  8. 计算app内部缓存文件大小

    #pragma mark - 计算单个文件大小 - (long long)fileSizeAtPath:(NSString*)filePath{ NSFileManager* manager = [N ...

  9. (一一七)基本文件操作 -SDWebImage清除缓存 -文件夹的大小计算

    在iOS的App沙盒中,Documents和Library/Preferences都会被备份到iCloud,因此只适合放置一些记录文件,例如plist.数据库文件.缓存一般放置到Library/Cac ...

随机推荐

  1. cocos2dx中的ScrollView

    ScrollView由视窗区域(裁剪区域)和内容区域组成,内容区域叫innerContainer. 视窗区域范围:get/setContentSize 内容区域:get/setInnerContain ...

  2. 掌握Thinkphp3.2.0----模型初步

    1.为什么要学习框架?框架是什么? 简单的说就是为了简单,提高开发的效率.至于什么是框架(一种规范),现在的我还不是很理解,容后再议. 学习框架最重要的就是遵循,按照开发者的意图来使用该框架. 2.t ...

  3. SQLSERVER2012 Audit (审核)功能

    数据库表结构和数据有时会被无意或者恶意,或者需要追踪最近的数据结构变更记录,以往必须通过日志查询,SQL Server2008开始提供了 审核(Audit )功能,SQL2012有所升级,利用它可以实 ...

  4. as3正则表达式

    1.新建正则表达式,有两种方式var exp1:RegExp = new RegExp("ABCD","g");var exp2 = /ABCD/g;//g g ...

  5. 选择App开发外包时,你该了解哪些法律常识?

    随着App需求的激增,选择App外包服务的客户也多了起来.然而客户和开发方对于其中的法律条款却不甚了解,导致在服务过程中,时常会发生一些分歧和纠纷,最终致使项目搁浅. 为了普及App外包的法律常识,移 ...

  6. 使用TreeView 使用多选功能

    1.要用TreeView多选就要显示复选框,TreeView默认不显示复选框,显示复选框: TreeView2.ShowCheckBoxes = TreeNodeTypes.All; 初始化TreeV ...

  7. (转载)(收藏)OceanBase深度解析

    一.OceanBase不需要高可靠服务器和高端存储 OceanBase是关系型数据库,包含内核+OceanBase云平台(OCP).与传统关系型数据库相比,最大的不同点, 是OceanBase是分布式 ...

  8. 如果空间不够的话,iOS发生这样的错误

    2016-12-16 10:24:50.945 gpxj[2634:21323] Simulator user has requested new graphics quality: 10 2016- ...

  9. jquery选择器和基本语句

    $("#aa"); //根据ID找 $(".aa"); //根据class找 $("div"); //根据标签名找 $("[id= ...

  10. 关于dll的一点收获

    蒙贾神指点. 对于kernel32.dll这种系统dll, 每一个进程都会加载一份, 映射到自己的进程空间. 实际上物理内存上还是只有一份dll. 如果对进程自己的dll进行修改, 这时操作系统会触发 ...