admin 管理员组

文章数量: 1184232

IOS 之 计算缓存,清除缓存

基本上所有的应用都会有清除缓存的功能,如果没有这项功能的话,手机负担会很大, 本文主要讲一下怎么实现查看缓存,清除缓存

1. 为了方便展示, 创建一个button

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    //设置页面背景图片

    self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"iPhone6-portrait@2x.png"]];

   self.view.alpha =0.6;

    

    self.button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];

    self.button1.backgroundColor = [UIColorclearColor];

   self.button1.frame =CGRectMake(25,200, 200, 40);

    [self.button1setBackgroundImage:[UIImageimageNamed:@"5.png"]forState:UIControlStateNormal];

    [self.button1addTarget:selfaction:@selector(clearCacheFlies)forControlEvents:UIControlEventTouchUpInside];

    [self.button1setTitleColor:[UIColordarkGrayColor] forState:UIControlStateNormal];

    [self.viewaddSubview:self.button1];

  

}


2. 在视图将要出现的时候, 计算有多少缓存

- (void)viewWillAppear:(BOOL)animated

{

    //清除缓存计算缓存大小

// 获取要清除缓存的路径

    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];

// 调用folderSizeAtPath方法计算缓存路径下的文件size

   CGFloat fileSize = [selffolderSizeAtPath:cachPath];

   NSLog(@"~~%f",fileSize);

   NSString *str = [NSStringstringWithFormat:@"清除缓存%.1fM", fileSize];

    [self.button1setTitle:str forState:UIControlStateNormal];

    self.button1.showsTouchWhenHighlighted =YES;

    

    NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES));

}


//二、计算缓存cache文件夹下文件大小。

//单个文件的大小

- (longlong)fileSizeAtPath:(NSString*) filePath{

    NSFileManager* manager = [NSFileManagerdefaultManager];

   if([manager fileExistsAtPath:filePath]){

        return[[managerattributesOfItemAtPath:filePath error:nil]fileSize];

    }

   return 0;

    

}



//遍历文件夹获得文件夹大小,返回多少M

- (float) folderSizeAtPath:(NSString*) folderPath{

    

    NSFileManager* manager = [NSFileManagerdefaultManager];

   if(![manager fileExistsAtPath:folderPath])

       return 0;

    

   NSEnumerator *childFilesEnumerator = [[managersubpathsAtPath:folderPath] objectEnumerator];

    

   NSString* fileName;

    

   long long folderSize =0;

    

   while((fileName = [childFilesEnumerator nextObject]) != nil){

       NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

        folderSize += [selffileSizeAtPath:fileAbsolutePath];

    }

   return folderSize/(1024.0*1024.0);//得到缓存大小M

    

}


//一、清理缓存cache文件夹,删除文件方法

-(void)clearCacheFlies

{

    UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"清除缓存成功"delegate:selfcancelButtonTitle:nilotherButtonTitles:@"确定",nil];

    [alertshow];

    [self.button1setTitle:@"清除缓存0M"forState:UIControlStateNormal];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];

       NSArray *files = [[NSFileManagerdefaultManager] subpathsAtPath:cachPath];

       NSLog(@"files :%ld",[filescount]);

       for (NSString *pin files) {

           NSError *error;

           NSString *path = [cachPath stringByAppendingPathComponent:p];

            if ([[NSFileManagerdefaultManager] fileExistsAtPath:path]) {

// 删除缓存

                [[NSFileManagerdefaultManager] removeItemAtPath:patherror:&error];

            }

           NSLog(@"清理成功!");

            

            

        }

        

    });

}


 看一下运行结果:
清除缓存之前



2.清除之后



本文标签: IOS 之 计算缓存 清除缓存