DsCacheUtils.m
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// DsCacheUtils.m
// DreamSleep
//
// Created by peter on 2022/4/25.
//
#import "DsCacheUtils.h"
@implementation DsCacheUtils
+ (NSString *)getCacheSize {
// 得到缓存路径
NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSFileManager * manager = [NSFileManager defaultManager];
CGFloat size = 0;
// 首先判断是否存在缓存文件
if ([manager fileExistsAtPath:path]) {
NSArray * childFile = [manager subpathsAtPath:path];
for (NSString * fileName in childFile) {
// 缓存文件绝对路径
NSString * absolutPath = [path stringByAppendingPathComponent:fileName];
size = size + [manager attributesOfItemAtPath:absolutPath error:nil].fileSize;
}
// 计算SDWebimage5的缓存和系统缓存总和
// size = size + [[SDImageCache sharedImageCache] totalDiskSize];
}
YYImageCache *cache = [YYWebImageManager sharedManager].cache;
NSInteger discCache = cache.diskCache.totalCost;
NSInteger memoryCache = cache.memoryCache.totalCost;
size = size + (discCache + memoryCache);
if (size < 0) { return @"0.0M"; }
double returnSize = size / 1024.0 / 1024.0;
return [NSString stringWithFormat:@"%.1fM", returnSize];
}
+ (void)cleanCacheWithSuccess:(void (^)(BOOL success))successBlock {
// 获取缓存路径
NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSFileManager * manager = [NSFileManager defaultManager];
// 判断是否存在缓存文件
if ([manager fileExistsAtPath:path]) {
NSArray * childFile = [manager subpathsAtPath:path];
// 逐个删除缓存文件
for (NSString *fileName in childFile) {
NSString * absolutPat = [path stringByAppendingPathComponent:fileName];
[manager removeItemAtPath:absolutPat error:nil];
}
// 清除yywebimage缓存
YYImageCache *cache = [YYWebImageManager sharedManager].cache;
[cache.memoryCache removeAllObjects];
[cache.diskCache removeAllObjects];
// 删除sdwebimage的缓存
// [[SDWebImageManager sharedManager].imageCache clearWithCacheType:SDImageCacheTypeAll completion:^{
// // 这里是又调用了得到缓存文件大小的方法,是因为不确定是否删除了所有的缓存,所以要计算一遍,展示出来
// if ([[self getCacheSize] isEqualToString:@"0.00M"]) {
// if (successBlock) {
// successBlock(YES);
// }
// [DSProgressHUD showToast:@"清除缓存成功"];
// } else {
// if (successBlock) {
// successBlock(NO);
// }
// [DSProgressHUD showToast:@"清除缓存失败"];
// }
// }];
}
}
@end