iOS清除緩衝,ios緩衝
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(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*)tableViewdidSelectRowAtIndexPath:(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-getfilesize
- (longlong)fileSizeAtPath:(NSString*)filePath{
NSFileManager*manager=[NSFileManagerdefaultManager];
if([managerfileExistsAtPath:filePath]){
return[[managerattributesOfItemAtPath:filePatherror:nil]fileSize];
}
return 0;
}
- (float)folderSizeAtPath:(NSString*)folderPath{
NSFileManager*manager=[NSFileManagerdefaultManager];
if(![managerfileExistsAtPath:folderPath])return 0;
NSEnumerator*childFilesEnumerator= [[managersubpathsAtPath:folderPath]objectEnumerator];
NSString*fileName;
longlongfolderSize= 0;
while((fileName=[childFilesEnumeratornextObject])!=nil){
NSString*fileAbsolutePath=[folderPathstringByAppendingPathComponent:fileName];
folderSize+=[selffileSizeAtPath:fileAbsolutePath];
}
returnfolderSize;
}