iOS清除缓存的方法
来源:互联网 发布:河鱼软件 编辑:程序博客网 时间:2024/05/16 14:08
//获取所有缓存大小
- (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];
}
}
});
}
0 0
- iOS清除缓存的方法
- iOS 缓存清除方法
- 清除ios的缓存
- IOS缓存基本知识和清除缓存的方法
- 【iOS】获取缓存大小清除缓存的方法
- 清除缓存的方法
- iOS中 SDWebImage手动清除缓存的方法 技术分享
- ios清除缓存的两种有效方法
- iOS中设置清除缓存功能的实现方法
- IOS 清除UIWebView的缓存
- iOS清除WebView的缓存
- ios 清除UIWebImage的缓存
- iOS清除WebView的缓存
- php 清除缓存的方法
- response方法清除缓存的
- 清除ajax缓存的方法
- varnish清除缓存的方法
- 清除ARP缓存的方法
- 网络多线程小结
- java之静态变量
- java读取properties配置文件问题
- oracle null和 ''的区别
- 2015年240个数据库引擎排名参考
- iOS清除缓存的方法
- ehCache基于JPA的二级缓存的使用
- ios 学习笔记(二)
- UIViewController的生命周期及iOS程序执行顺序
- An Architecture for Fast and General Data Processing on Large Clusters
- 清华EMBA课程系列思考之二 -- 清华探究及信息战略与管理
- jquery Datatable参数及使用方法
- Swift Cheat Sheet [2] - Control Flow
- Int VS Integer