import
if(indexPath.row==1){
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"确定清除缓存%.2fM", [self getCacheSize]] delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
[sheet showInView:self.view];
}
//获得缓存文件的大小
// 得到缓存文件有多少兆
- (double)getCacheSize {
// 在使用SDWebImage下载图片的时候,SD会把图片缓存到本地(作为图片文件存到沙盒里面),SD自带了清除缓存的机制
// NSLog(@"%@", NSHomeDirectory());
// 1bit, 即1个b 就是1个1或1个0 代表一个二进制位,bit是计算机存储的最小单位
// 1个字节(Byte),即1个B, 就是8个bit, 1Byte = 8bit, 一般用Byte表示计算机存储的基本单位
// 1个千字节(KB), 即1024个Byte, 即 1024个B, 即2^10个字节
// 1个兆字节(M, MB, Megabytes),即1024个千字节,即2^10个千字节 1M = 2^10 KB = 2^20 B = 2^20*8 bit
// 1个吉字节(G, GB, GigaByte), 即1024个兆字节,即2^10个兆字节
// 1个太字节(T, TB, TeraByte), 即1024个吉字节,即2^10个G字节
// 1个拍字节(P, PB, PetaByte), 即1024个T字节,即2^10个T字节
// 1个艾字节(E, EB, ExaByte), 即1024个P字节,即2^10个P字节
// 1个泽它字节(Z, ZB, ZettaByte), 即1024个E字节,即2^10个E字节
// 1个尧它字节(Y, YB, YaoitByte), 即1024个Z字节,即2^10个Z字节
// 注意,换算方式也有以1000为单位的,所以一般的硬盘生产厂商采用1000为进制的换算, 故有其报告的容量比硬盘标示的容量小的情况发生, 没什么大惊小怪的
// 获取缓存
// SDWebImage自身下载图片有缓存
SDImageCache *imageCache = [SDImageCache sharedImageCache];
NSUInteger fileSize = [imageCache getSize]; // 以字节为单位
// 本地下载的缓存(我们自己搞的缓存)
//myCache 路径缓存
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *myCache = [cachePath stringByAppendingPathComponent:@"myCache"];
// 获取指定文件信息
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *fileInfo = [fm attributesOfItemAtPath:myCache error:nil];
// SD图片缓存加上我们自己的缓存
fileSize += fileInfo.fileSize;
// 以兆为单位返回
return fileSize/1024.0/1024.0;
}
//ActionSheet的代理方法
#pragma mark - <UIActionSheetDelegate>
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { // 确定清除
[self removeCacheData];
}
}
//去除缓存数据
- (void)removeCacheData {
// SD清空缓存(实际上就是把缓存在本地的图片删除掉)
[[SDImageCache sharedImageCache] clearDisk];
// 本地的清除缓存
//myCache 路径缓存
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *myCache = [cachePath stringByAppendingPathComponent:@"myCache"];
[[NSFileManager defaultManager] removeItemAtPath:myCache error:nil];
}