今天项目中要用到缓存的计算和清理,由于现在iOS就我一个小虾米,so,虽然自己以前没有写过,好了,多的不说,开始代码。
首先
惯例,代码的demo地址:https://github.com/sunyunfei/Clear-Cache.git
接着我要说明一下,方法是我在网上苦苦寻找的,然后经过自己的实践确定是对的,可行的所以整合出来,不要说我抄人家的哦哦,其实某一个功能的代码就是这些,没有所谓的这些,继续。
我是写了一个清除类,可以直接调用。
.h
#import <UIKit/UIKit.h>
@interface ClearCache : NSObject
+(float)fileSizeAtPath:(NSString *)path;
+(float)folderSizeAtPath:(NSString *)path;
+(void)clearCache:(NSString *)path;
@end
.m
#import "ClearCache.h"
#import <SDWebImage/UIImageView+WebCache.h>
@implementation ClearCache
/**
* 缓存文件路径
*
* //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//NSString *cacheDirectory = [paths objectAtIndex:0];
//NSString *filePath =[documentsDirectory stringByAppendingPathComponent:fileName];
*/
/**
* 计算文件大小
*
* @param path <#path description#>
*
* @return <#return value description#>
*/
+(float)fileSizeAtPath:(NSString *)path{
NSFileManager *fileManager=[NSFileManager defaultManager];
if([fileManager fileExistsAtPath:path]){
long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
return size/1024.0/1024.0;
}
return 0;
}
/**
* 计算目录大小
*
* @param path <#path description#>
*
* @return <#return value description#>
*/
+(float)folderSizeAtPath:(NSString *)path{
NSFileManager *fileManager=[NSFileManager defaultManager];
float folderSize;
if ([fileManager fileExistsAtPath:path]) {
NSArray *childerFiles=[fileManager subpathsAtPath:path];
for (NSString *fileName in childerFiles) {
NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
folderSize +=[self fileSizeAtPath:absolutePath];
}
//SDWebImage框架自身计算缓存的实现
folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
return folderSize;
}
return 0;
}
/**
* 清除文件缓存
*
* @param path <#path description#>
*/
+(void)clearCache:(NSString *)path{
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
NSArray *childerFiles=[fileManager subpathsAtPath:path];
for (NSString *fileName in childerFiles) {
//如有需要,加入条件,过滤掉不想删除的文件
NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
[fileManager removeItemAtPath:absolutePath error:nil];
}
}
[[SDImageCache sharedImageCache] cleanDisk];
}
@end
重点
都在这一个类里面,就是采用系统的一个NSFileManager类,进行的一系列操作。
然后我在控制器进行一个测试
NSString *path = @"http://pic.nipic.com/2007-10-25/2007102585630254_2.jpg";
UIImageView *image = [[UIImageView alloc]initWithFrame:self.view.bounds];
[image sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
[self.view addSubview:image];
加载一张图片,用的sdwebimage框架,大家都知道这个框架是有缓存的,所以我输出缓存的路径去查看:
//找到缓存文件路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//输出缓存路径
NSLog(@"%@",documentsDirectory);
找到路径:
确实有着一张图片缓存,然后调用写好的类方法:
//计算缓存文件大小
CGFloat count = [ClearCache folderSizeAtPath:documentsDirectory];
NSLog(@"%.2f",count);
// 清除缓存
[ClearCache clearCache:documentsDirectory];
运行结果:
可以看到,缓存已经被清理。
大家可以下载demo好好看一下。