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;
}
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 缓存大小, 清除缓存 成 封装工具
- ios webview清除缓存
- IOS 清除UIWebView的缓存
- iOS 清除缓存
- iOS经典讲解之清除缓存文件
- iOS 清除缓存
- iOS_清除xcode编译器的缓存
- iOS 清除缓存
- iOS清除xcode缓存和生成文件
- iOS清除缓存
- iOS之清除缓存
- iOS中显示获取缓存的大小多少M,点击清除缓存方法 (2016-03-03 12:12:01)
- iOS //清除本地缓存
- iOS开发 - Swift实现清除缓存功能
- iOS开发清除缓存功能 - 芒果iOS
- ios webview清除缓存
- iOS清除缓存详细解析、app文件路径操作,自己个人对于沙盒的理解
- ios webview清除缓存Cookie
- ioS 计算缓存和清除缓存
- iOS学习笔记-127.SDWebImage3——缓存的清除和操作的取消