//获取所有缓存大小
- (double)getCachesSize {
//1.自定义的缓存 2.sd 的缓存
NSUInteger sdFileSize = [[SDImageCache sharedImageCache] getSize];
//先获取 系统 Library/Caches 路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] ;
NSString *myCachesPath = [caches stringByAppendingPathComponent:@"LWXCarsCaches"];
//遍历 自定义缓存文件夹
//目录枚举器 ,里面存放着 文件夹中的所有文件名
NSDirectoryEnumerator *enumrator = [[NSFileManager defaultManager] enumeratorAtPath:myCachesPath];
NSUInteger mySize = 0;
//遍历枚举器
for (NSString *fileName in enumrator) {
//文件路径
NSString *filePath = [myCachesPath stringByAppendingPathComponent:fileName];
NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
//获取大小
mySize += fileDict.fileSize;//字节大小
}
//1M == 1024KB == 1024*1024bytes
return (mySize+sdFileSize)/1024.0/1024.0;
}
//点击 操作表单的按钮
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.destructiveButtonIndex) {
//删除
//1.删除sd
[[SDImageCache sharedImageCache] clearMemory];//清除内存缓存
//2.清除 磁盘缓存
[[SDImageCache sharedImageCache] clearDisk];
//清除自己的缓存
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] ;
NSString *myCachesPath = [caches stringByAppendingPathComponent:@"LWXCarsCaches"];
//删除
[[NSFileManager defaultManager] removeItemAtPath:myCachesPath error:nil];
}
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
- (IBAction)clearBtn:(id)sender {
//清理缓存...
UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"总共有%.5fM缓存",[self getCacheSize]] preferredStyle:UIAlertControllerStyleActionSheet];
__weak typeof(self)weakSelf = self;
[sheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
{
[[SDImageCache sharedImageCache] clearMemory];
[[SDImageCache sharedImageCache] clearDisk];
//
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
[[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
self.dataLab.text = [NSString stringWithFormat:@"%.5fM",[weakSelf getCacheSize]];
}]];
[self presentViewController:sheet animated:YES completion:nil];
//NSLog(@"%@",[NSString stringWithFormat:@"%.5fM",[self getCacheSize]]);
//stringWithFormat:@"%dM", 0];
}
#pragma mark - 计算缓存大小
- (CGFloat)getCacheSize {
NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
__block NSUInteger count = 0;
for (NSString *fileName in enumerator) {
NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
count += fileDict.fileSize;
}
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize; //返回M
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
-(void)clearCache{
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
NSLog(@"files :%ld",[files count]); // 文件夹的数量
for (NSString *p in files) {
NSError *error;
NSString *path1 = [cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path1]) {
[[NSFileManager defaultManager] removeItemAtPath:path1 error:&error];
}
}
});
}
- (double)getCachesSize {
//1.自定义的缓存 2.sd 的缓存
NSUInteger sdFileSize = [[SDImageCache sharedImageCache] getSize];
//先获取 系统 Library/Caches 路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] ;
NSString *myCachesPath = [caches stringByAppendingPathComponent:@"LWXCarsCaches"];
//遍历 自定义缓存文件夹
//目录枚举器 ,里面存放着 文件夹中的所有文件名
NSDirectoryEnumerator *enumrator = [[NSFileManager defaultManager] enumeratorAtPath:myCachesPath];
NSUInteger mySize = 0;
//遍历枚举器
for (NSString *fileName in enumrator) {
//文件路径
NSString *filePath = [myCachesPath stringByAppendingPathComponent:fileName];
NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
//获取大小
mySize += fileDict.fileSize;//字节大小
}
//1M == 1024KB == 1024*1024bytes
return (mySize+sdFileSize)/1024.0/1024.0;
}
//点击 操作表单的按钮
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.destructiveButtonIndex) {
//删除
//1.删除sd
[[SDImageCache sharedImageCache] clearMemory];//清除内存缓存
//2.清除 磁盘缓存
[[SDImageCache sharedImageCache] clearDisk];
//清除自己的缓存
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] ;
NSString *myCachesPath = [caches stringByAppendingPathComponent:@"LWXCarsCaches"];
//删除
[[NSFileManager defaultManager] removeItemAtPath:myCachesPath error:nil];
}
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
- (IBAction)clearBtn:(id)sender {
//清理缓存...
UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"总共有%.5fM缓存",[self getCacheSize]] preferredStyle:UIAlertControllerStyleActionSheet];
__weak typeof(self)weakSelf = self;
[sheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
{
[[SDImageCache sharedImageCache] clearMemory];
[[SDImageCache sharedImageCache] clearDisk];
//
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
[[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
self.dataLab.text = [NSString stringWithFormat:@"%.5fM",[weakSelf getCacheSize]];
}]];
[self presentViewController:sheet animated:YES completion:nil];
//NSLog(@"%@",[NSString stringWithFormat:@"%.5fM",[self getCacheSize]]);
//stringWithFormat:@"%dM", 0];
}
#pragma mark - 计算缓存大小
- (CGFloat)getCacheSize {
NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
__block NSUInteger count = 0;
for (NSString *fileName in enumerator) {
NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
count += fileDict.fileSize;
}
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize; //返回M
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
-(void)clearCache{
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
NSLog(@"files :%ld",[files count]); // 文件夹的数量
for (NSString *p in files) {
NSError *error;
NSString *path1 = [cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path1]) {
[[NSFileManager defaultManager] removeItemAtPath:path1 error:&error];
}
}
});
}