1. 计算 沙盒 缓存大小 ,可能是 耗时 操作,放入子线程
- (void)viewDidLoad {
[super viewDidLoad];
// 沙盒路径 获取
NSLog(@"%@",NSHomeDirectory());
// 计算 缓存大小,可能是 耗时 操作,放入子线程
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// library/cache
NSString* cachePath= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
// 获取文件管理者
NSFileManager* mgr= [NSFileManager defaultManager];
NSString* dirpath= [cachePath stringByAppendingPathComponent:@"mp3"];
NSLog(@"dirpath=%@",dirpath);
// 是否为文件夹
BOOL isDirectory=NO;
BOOL exists= [mgr fileExistsAtPath:dirpath isDirectory:&isDirectory];
if(!exists){
NSLog(@"%@",@"目录不存在");
return ;
}
// 获取 文件、文件夹 属性
NSDictionary* attrss= [mgr attributesOfItemAtPath:dirpath error:nil];
/**
attrss={
NSFileCreationDate = "2020-07-11 02:33:52 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = "2020-07-11 02:35:01 +0000";
NSFileOwnerAccountID = 501;
NSFilePosixPermissions = 493;
NSFileReferenceCount = 5;
NSFileSize = 160;
NSFileSystemFileNumber = 8603496038;
NSFileSystemNumber = 16777220;
NSFileType = NSFileTypeDirectory; 文件夹类
NSFileTypeRegular: 文件类型
}
*/
NSLog(@"attrss=%@",attrss);
NSInteger size=0;
if(isDirectory){
NSArray* subpaths= [mgr subpathsAtPath:dirpath];
for(int i=0;i<subpaths.count;i++){
// 全路径
NSString* fullSubpath= [dirpath stringByAppendingPathComponent:subpaths[i]];
// 文件属性
NSDictionary* attrs= [mgr attributesOfItemAtPath:fullSubpath error:nil];
// 把所有大小 累加起来
size= size + [attrs[NSFileSize] unsignedIntegerValue];
}
// mac 下计算 kb 是 除以 1000 来计算
NSLog(@"dict->size=%ld",size);
}else{
size = size + [mgr attributesOfItemAtPath:dirpath error:nil].fileSize;
NSLog(@"file->size=%ld",size);
}
dispatch_async(dispatch_get_main_queue(), ^{
// 更新UI 回到主队列
NSLog(@"file->size=%ld",size);
});
});
}
清理缓存功能:
1. 把 Library/Caches下 sdwebimages 和 自己的
文件夹路径 下, 下载的文件 获取 然后清除
2. 不属于 自己的东西 文件目录不要 去清除,
避免出现问题
把上面代码封装到分类中实现:
NSString+XMGExtension.h
#import <Foundation/Foundation.h>
@interface NSString (XMGExtension)
- (unsigned long long)fileSize;
@end
NSString+XMGExtension.m
//
// NSString+XMGExtension.m
// 5期-百思不得姐
//
// Created by xiaomage on 15/11/16.
// Copyright © 2015年 xiaomage. All rights reserved.
//
#import "NSString+XMGExtension.h"
@implementation NSString (XMGExtension)
//- (unsigned long long)fileSize
//{
// // 总大小
// unsigned long long size = 0;
//
// // 文件管理者
// NSFileManager *mgr = [NSFileManager defaultManager];
//
// // 文件属性
// NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];
//
// if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) { // 文件夹
// // 获得文件夹的大小 == 获得文件夹中所有文件的总大小
// NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
// for (NSString *subpath in enumerator) {
// // 全路径
// NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
// // 累加文件大小
// size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
// }
// } else { // 文件
// size = attrs.fileSize;
// }
//
// return size;
//}
- (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;
}
@end
cell 封装:
XMGClearCacheCell.h
#import <UIKit/UIKit.h>
@interface XMGClearCacheCell : UITableViewCell
@end
XMGClearCacheCell.m
#import "XMGClearCacheCell.h"
#import "NSString+XMGExtension.h"
//#import <SDImageCache.h>
//#import <SVProgressHUD.h>
//#define XMGCustomCacheFile [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Custom"]
#define XMGCustomCacheFile @"/Users/denganzhi/Desktop/内容"
@implementation XMGClearCacheCell
- (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 XMGClearCacheCell * weakSelf = self;
__weak typeof(self) weakSelf = self;
// 在子线程计算缓存大小
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:2.0];
// 获得缓存文件夹路径
unsigned long long size = XMGCustomCacheFile.fileSize;
// size += [SDImageCache sharedImageCache].getSize;
[NSThread sleepForTimeInterval:10.0];
// 如果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:@"%lluB", size];
}
// 生成文字
NSString *text = [NSString stringWithFormat:@"清除缓存(%@)", sizeText];
// 回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 设置文字
weakSelf.textLabel.text = text;
// 清空右边的指示器,必须 清空才设置 新的指示器
// 因为 accessoryView 优先级 高于 accessoryType
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:XMGCustomCacheFile error:nil];
[mgr createDirectoryAtPath:XMGCustomCacheFile withIntermediateDirectories:YES attributes:nil error:nil];
// 所有的缓存都清除完毕
dispatch_async(dispatch_get_main_queue(), ^{
// 隐藏指示器
// [SVProgressHUD dismiss];
// 设置文字
self.textLabel.text = @"清除缓存(0B)";
});
});
// }];
}
/**
* 当cell重新显示到屏幕上时, 也会调用一次layoutSubviews
*
* 当cell 消失的时候动画会 停止 , 当cell 出现的时候 不会调用 initWithStyle方法,而是 会调用layoutSubviews 方法,重新启动动画
*/
- (void)layoutSubviews
{
[super layoutSubviews];
// cell重新显示的时候, 继续转圈圈
UIActivityIndicatorView *loadingView = (UIActivityIndicatorView *)self.accessoryView;
[loadingView startAnimating];
}
@end
cell 使用:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
XMGClearCacheCell* xmgclear= [[XMGClearCacheCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier: nil];
return xmgclear;
}
效果图:
源码地址: https://download.csdn.net/download/dreams_deng/12614145