iOS清除缓存

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

}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。