计算缓存大小:
- (void)setCachesLabel
{
UILabel * fileSizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(VIEWWIDTH - 10, 10, 80, 20)];
fileSizeLabel.font = SLCellTextFont;
fileSizeLabel.textColor = SLCellTextColor;
fileSizeLabel.textAlignment = NSTextAlignmentRight;
if(cacheFilesSize / (1024*1024) < 1)
{
fileSizeString = [NSString stringWithFormat:@"%dKB",(int)(cacheFilesSize/1024)];
}
else
{
fileSizeString = [NSString stringWithFormat:@"%.1fM",(cacheFilesSize/(1024*1024))];
}
fileSizeLabel.text = fileSizeString;
}
- (float)cacheFilesSize
{
//获取沙盒中cache文件夹的路径
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSFileManager* manager = [NSFileManager defaultManager];
//在cache文件夹下是否有子文件夹存在,没有的话返回0
if (![manager fileExistsAtPath:cachPath]) return 0;
//将caches子文件夹数组转换为枚举器,以便得到cache文件夹中的每一个子文件件(包括子文件夹的子文件夹)
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:cachPath] objectEnumerator];
NSString* fileName;
long long folderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil){
//拼接cache文件夹下每一个子文件夹的绝对路径。
NSString* fileAbsolutePath = [cachPath stringByAppendingPathComponent:fileName];
//将每一个子文件夹中的内容大小加起来。
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize;
/*
NSEnumerator:NSEnumerator is an abstract class, instances of whose subclasses enumerate collections of other objects, such as arrays and dictionaries.
NSEnumerator是一个抽象类,其子类对象枚举了其他对象的集合,例如数组和字典。
objectEnumerator:Returns an enumerator object that lets you access each object in the array.
返回一个枚举器对象从而可以让你获取数组中的每一个对象。
*/
}
- (long long) fileSizeAtPath:(NSString*) filePath
{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
/* fileSize: This method returns the file’s size.
If the file has a resource fork, the returned value does not include the size of the resource fork.
fileSize方法可以返回一个文件的大小,但是不包括文件夹中子文件夹的大小,所以计算缓存时要把cache文件夹下所有的子文件夹遍历一遍。*/
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
long long size = [[manager attributesOfItemAtPath:cachPath error:nil] fileSize];
SFun_Log(@"")
return 0;
}
清除缓存:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100) {
if (buttonIndex==1) {
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
//此方法能够获取caches文件夹下所有的子文件夹,子子文件夹。
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
NSLog(@"files :%d",[files count]);
for (NSString *p in files) {
NSError *error;
NSString *path = [cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
}
CGRect rect=CGRectMake(100, 200 , VIEWWIDTH-200, 40);
[UIToastView showToastViewWithContent:@"清除缓存成功" andRect:rect andTime:1 andObject:self];
[_moreTableView reloadData];
}
}
}