内存管理_清除缓存

来源:互联网 发布:rt809h编程器 编辑:程序博客网 时间:2024/05/18 06:25

    float clearCacheName;   //用来展示缓存大小的字符串


- (void)viewDidLoad{


    [self getCasheSize];


    [self initContentView];

}

//获取缓存大小

- (void)getCasheSize

{

    //缓存

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

    clearCacheName= [self folderSizeAtPath:cachPath];

}


//遍历文件夹获得文件夹大小,返回多少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);

}



//单个文件的大小

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

    NSFileManager* manager = [NSFileManager defaultManager];

    if ([manager fileExistsAtPath:filePath]){

        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

    }

    return 0;

}


alertView-delegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 1 && alertView.tag == 1011) {

        [self clearTmpPics];

        

    }

}


//清除缓存

- (void)clearTmpPics

{

    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];

                       

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

                       

                       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

{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"

                                                        message:@"缓存清理成功!"

                                                       delegate:nil

                                              cancelButtonTitle:@"确定"

                                              otherButtonTitles:nil, nil];

    [alertView show];

    

    [self getCasheSize];

    [self initContentView];

    [myTableView reloadData];

}

0 0
原创粉丝点击