清除缓存步骤与显示弹框
// 单例(一个应用程序只有一个对象)
SDImageCache *sdImageCache = [SDImageCache sharedImageCache];
NSString *str = [NSString stringWithFormat:@"缓存大小%.2fM.是否清除缓存?", [sdImageCache checkTmpSize]];
NSString *cancelStr = @"取消";
if ([sdImageCache checkTmpSize] == 0) {
str = @"您还没有缓存, 不需要清理哦!";
cancelStr = nil;
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:cancelStr otherButtonTitles:@"确定", nil];
// 这里执行协议方法的对象是这个视图控制器对象(协议方法是给别人用的)
alertView.delegate = self;
[alertView show];
[alertView release];
// alertView协议方法执行清除缓存的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
if (buttonIndex == 1) {
[[SDImageCache sharedImageCache] clearDisk];
}
}
// 计算缓存文件大小方法的实现
- (float)checkTmpSize {
float totalSize = 0;
NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:_diskCachePath];
for (NSString *fileName in fileEnumerator) {
NSString *filePath = [_diskCachePath stringByAppendingPathComponent:fileName];
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
unsigned long long length = [attrs fileSize];
totalSize += length / 1024.0 / 1024.0;
} // NSLog(@"tmp size is %.2f",totalSize);
return totalSize;
}