清除缓存数据的方法

来源:互联网 发布:linux下启动tomcat 编辑:程序博客网 时间:2024/05/16 06:28

我们在做项目的时候,常常需要一个清除数据缓存的功能,下面的一些代码是我平常用的清除缓存的方法

#import "UIImageView+WebCache.h"#import "SDImageCache.h"//包含头文件   借助于SDWebImage库//创建一个buttonUIButton *DelButton=[UIButton buttonWithType:UIButtonTypeCustom];    DelButton.frame=CGRectMake(40, 100, 60, 40);    [DelButton addTarget:self action:@selector(DelButton) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:DelButton];
-(void)DelButton{UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"删除缓存文件:%.2fM",[self getCachesSize]] delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:nil];            [sheet showInView:self.view];}
-(double)getCachesSize{    //获取缓存数据    double sdSize=[[SDImageCache sharedImageCache] getSize];    NSString *myCachePath=[NSHomeDirectory() stringByAppendingFormat:@"/Library/Caches/MyCaches"];    NSDirectoryEnumerator *enumerator=[[NSFileManager defaultManager] enumeratorAtPath:myCachePath];    double mySize=0;    for (NSString *fileName in enumerator) {        NSString *filePath=[myCachePath stringByAppendingPathComponent:fileName];        NSDictionary *dict=[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];        mySize +=dict.fileSize;    }    double totalSize=sdSize/1024/1024;//转化为M    return totalSize;}-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{//清除缓存    if (buttonIndex==actionSheet.destructiveButtonIndex) {        [[SDImageCache sharedImageCache] clearMemory];        [[SDImageCache sharedImageCache] clearDisk];        NSString *myCachePath=[NSHomeDirectory() stringByAppendingFormat:@"/Library/Caches/MyCaches"];        [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];    }}
0 0
原创粉丝点击