计算缓存文件大小
- (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@interface JGClearCacheCell : UITableViewCell@end//// JGClearCacheCell.m// //// Created by JG on 16/12/08.// Copyright © 2016年 JG. All rights reserved.//#import "JGClearCacheCell.h"#import #import #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