您的位置:首页 > 移动开发 > IOS开发

iOS清除缓存

2015-05-02 14:39 176 查看
-(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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: