首页 > 移动平台 > 详细

iOS清除缓存

时间:2015-05-02 15:10:59      阅读:268      评论:0      收藏:0      [点我收藏+]

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

staticNSString*identifier =@"myCell";

    UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:identifier];

    

  if(cell ==nil){

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:identifier];

    }

cell.textLabel.text=@"清除缓存";

  NSString*bundleId=[[[NSBundlemainBundle]infoDictionary]objectForKey:@"CFBundleIdentifier"];

    NSArray*paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

    NSString*path=[[pathsobjectAtIndex:0]stringByAppendingPathComponent:bundleId];

    CGFloatsize =[selffolderSizeAtPath:path];

    if(size> 10)

    {

       if(size< 1000)

      {

          cell.detailTextLabel.text=[NSStringstringWithFormat:@"%.0fB",size];

       } else if(size< 1000 * 1000){

          cell.detailTextLabel.text=[NSStringstringWithFormat:@"%.2fKB",size/ 1000];

       }else{

          cell.detailTextLabel.text= [NSStringstringWithFormat:@"%.2fMB",size/ 1000 / 1000];

       }

    }else{

      cell.detailTextLabel.text= @"";

    }

}



-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

NSString*bundleId=[[[NSBundlemainBundle]infoDictionary]objectForKey:@"CFBundleIdentifier"];

  NSArray*paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

    NSString*path=[[pathsobjectAtIndex:0]stringByAppendingPathComponent:bundleId];

    [[NSFileManagerdefaultManager]removeItemAtPath:patherror:nil];

  

    UIAlertView*alertCache=[[UIAlertViewalloc]initWithTitle:nilmessage:@"缓存清楚成功"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];

    [alertCacheshow];

    [tableViewreloadData];

}


计算文件大小

#pragmamark -get filesize

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

    NSFileManager*manager=[NSFileManagerdefaultManager];

    if([manager fileExistsAtPath:filePath]){

        return[[managerattributesOfItemAtPath:filePatherror:nil]fileSize];

    }

    return 0;

}

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

    NSFileManager*manager=[NSFileManagerdefaultManager];

    if(![manager fileExistsAtPath:folderPath])return 0;

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

    NSString*fileName;

    longlongfolderSize = 0;

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

        NSString*fileAbsolutePath =[folderPath stringByAppendingPathComponent:fileName];

        folderSize+= [selffileSizeAtPath:fileAbsolutePath];

    }

    returnfolderSize;

}


iOS清除缓存

原文:http://blog.csdn.net/huanghaiyan_123/article/details/45439825

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!