计算缓存大小和清除缓存

计算缓存大小:

- (void)setCachesLabel
{
    UILabel * fileSizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(VIEWWIDTH - 10, 10, 80, 20)];
    fileSizeLabel.font = SLCellTextFont;
    fileSizeLabel.textColor = SLCellTextColor;
    fileSizeLabel.textAlignment = NSTextAlignmentRight;
    if(cacheFilesSize / (1024*1024) < 1)
    {
        fileSizeString = [NSString stringWithFormat:@"%dKB",(int)(cacheFilesSize/1024)];
    }
    else
    {
        fileSizeString = [NSString stringWithFormat:@"%.1fM",(cacheFilesSize/(1024*1024))];
    }
    fileSizeLabel.text = fileSizeString;
}
- (float)cacheFilesSize
{
    //获取沙盒中cache文件夹的路径
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSFileManager* manager = [NSFileManager defaultManager];
    
    //在cache文件夹下是否有子文件夹存在,没有的话返回0
    if (![manager fileExistsAtPath:cachPath]) return 0;

    //将caches子文件夹数组转换为枚举器,以便得到cache文件夹中的每一个子文件件(包括子文件夹的子文件夹)
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:cachPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        //拼接cache文件夹下每一个子文件夹的绝对路径。
        NSString* fileAbsolutePath = [cachPath stringByAppendingPathComponent:fileName];
        //将每一个子文件夹中的内容大小加起来。
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize;
    
    /*
    NSEnumerator:NSEnumerator is an abstract class, instances of whose subclasses enumerate collections of other objects, such as arrays and dictionaries.
    NSEnumerator是一个抽象类,其子类对象枚举了其他对象的集合,例如数组和字典。
     
     objectEnumerator:Returns an enumerator object that lets you access each object in the array.
     返回一个枚举器对象从而可以让你获取数组中的每一个对象。
     */
}
- (long long) fileSizeAtPath:(NSString*) filePath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        /* fileSize: This method returns the file’s size.
        If the file has a resource fork, the returned value does not include the size of the resource fork.
        fileSize方法可以返回一个文件的大小,但是不包括文件夹中子文件夹的大小,所以计算缓存时要把cache文件夹下所有的子文件夹遍历一遍。*/
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    long long size = [[manager attributesOfItemAtPath:cachPath error:nil] fileSize];
    SFun_Log(@"")
    return 0;
    
}

清除缓存:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==100) {
        if (buttonIndex==1) {
            
            NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
            
            //此方法能够获取caches文件夹下所有的子文件夹,子子文件夹。
            NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
            NSLog(@"files :%d",[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];
                }
            }
            CGRect rect=CGRectMake(100, 200 , VIEWWIDTH-200, 40);
            [UIToastView showToastViewWithContent:@"清除缓存成功" andRect:rect andTime:1 andObject:self];
            [_moreTableView reloadData];
        }
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值