app中基本上都要有清除缓存功能,下面是自己写的一个清除缓存的事例,有需要的可以看下。大神勿喷,谢谢!
效果图如下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];
if (cell == nil)
{
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TableCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.detailTextLabel.font=[UIFont systemFontOfSize:14];
}
if (indexPath.row==0) {
cell.textLabel.text = @"清理缓存";
cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2fM",[self filePath]];
}else{
cell.imageView.image = [UIImage imageNamed:@"206040274942060663"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row==0) {
//清除缓存
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
message:@"是否清理缓存?"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
[alertView show];
}
}
// 显示缓存大小
-( float )filePath
{
NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
return [ self folderSizeAtPath :cachPath];
}
//1:首先我们计算一下 单个文件的大小
- ( long long ) fileSizeAtPath:( NSString *) filePath{
NSFileManager * manager = [ NSFileManager defaultManager ];
if ([manager fileExistsAtPath :filePath]){
return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
}
return 0 ;
}
//2:遍历文件夹获得文件夹大小,返回多少 M
- ( 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 );
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1 ) {
[self clearFile];
}
}
// 清理缓存
- (void)clearFile
{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
, ^{//地址
NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
NSLog ( @"cachpath = %@" , cachPath);
for ( NSString * p in files) {
NSError * error = nil ;
NSString * path = [cachPath stringByAppendingPathComponent :p];
if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
[[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
}
}
[ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];});
}
-(void)clearCachSuccess
{
NSLog ( @" 清理成功 " );
NSIndexPath *index=[NSIndexPath indexPathForRow:0 inSection:0];//刷新某一行数据
[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];
}
代码通道:https://github.com/Gang679/GZBase 记得Star一下,谢谢!