在TableView中添加清理缓存的功能
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
[self folderSizeAtPath:cachPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"cell1";//static
MSThirdViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell=[[[MSThirdViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];//cell 用cell来显示;
}
cell.MLable.text = [arr objectAtIndex:indexPath.row];
cell.MLable.font = [UIFont fontWithName:@"Arial" size:14];
cell.Mimage.image = [UIImage imageNamed:[image objectAtIndex:indexPath.row]];
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
cell.MCache.text = [NSString stringWithFormat:@"(%.2fM)",[self folderSizeAtPath:cachPath]];
return cell;
}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MSThirdViewCell * cell = (MSThirdViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
[self clear];
_alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"正在瘦身....." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[_alert show];
cell.MCache.text = [NSString stringWithFormat:@"(%.2fM)",[self folderSizeAtPath:cachPath]];
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
}
-(void) performDismiss:(NSTimer *)timer
{
[_alert dismissWithClickedButtonIndex:0 animated:NO];
[_alert release];
}
- (void)clear
{
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];
for (NSString *p in files) {
NSError *error;
NSString *path = [cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
}
[self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
}
-(void)clearCacheSuccess
{
NSLog(@"清理成功");
}
//计算某个路径下的缓存大小
- (float ) folderSizeAtPath:(NSString*)folderPath {
NSFileManager* manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:folderPath]) return 0;
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
NSString* fileName;
long long folderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil){
NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize/(1024.0*1024.0);
}
//计算某个文件的缓存大小
- (long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}