计算缓存文件大小

- (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. 合并多个工作薄workbooks到一个工作薄workbook

    微软示例教程 微软示例教程 Sub MergeAllWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim NRo ...

  2. openstack中eventlet使用

    openstack中使用eventlet的协程来实现并发. 第一种,使用eventlet.GreenPool来管理绿色线程 如l3-agent在开启了8个绿色线程来处理router消息 def _pr ...

  3. 深入解析PHP中逗号与点号的区别

    大部分同学都知道逗号要比点号快,但就是不知道为什么,更不知道逗号与点号这两者之间到底有什么区别.下面小编就来详细的为大家介绍一下,需要的朋友可以过来参考下 echo 'abc'.'def'; //用点 ...

  4. vert.x学习(二),使用Router来定义用户访问路径

    这里需要用到vertx-web依赖了,依然是在pom.xml里面导入 <?xml version="1.0" encoding="UTF-8"?> ...

  5. C和C++混合编程中的extern "C" {}

    引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: 1 2 3 4 5 6 7 8 9 #ifdef __cplusplus extern "C" { #endif ...

  6. C# MDI子窗体互相操作

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. 如何理解C#委托

    一:从下面的例子开始,理解委托变量本质 如上图,Condition是我定义的委托变量.这个委托变量的本质就是地址变量(即C语言当中的指针变量),它保存的是方法的入口地址. 当函数的调用者传递实参给这个 ...

  8. Spring MVC配置

    web配置 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="ht ...

  9. 16C554(8250)驱动分析

    参考: http://www.cnblogs.com/zym0805/p/4815041.html 一. 硬件数据手册 The ST16C554D is a universal asynchronou ...

  10. 经历alidns在国外的严重延时

    有个域名,是在国外1und1申请的,但dns的解析,国外的空间的功能弱爆了. 之前是放在dnspod,后来又试过dnspod的海外, 最后放回alidns,之前一直都很好的. 这2天国内没问题,在德国 ...