码迷,mamicode.com
首页 > 移动开发 > 详细

iOS清除缓存

时间:2015-05-02 15:13:04      阅读:206      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!