NSString 继续分类的介绍(清除缓存)
来源:互联网 发布:ubuntu如何添加中文 编辑:程序博客网 时间:2024/06/10 06:59
NSString 的分类 计算文件夹或者文件的大小
一般用于清除缓存计算文件大小时候可以用到
可以是 路径.fileSize 或者是具体文件 如:/a.png.fileSize 就可以计算出大小
具体代码如下:
// - (NSInteger)fileSize
{
// 文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 是否为文件夹
BOOL isDirectory = NO;
// 这个路径是否存在
BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
// 路径不存在
if (exists == NO) return 0;
if (isDirectory) { // 文件夹 // 总大小 NSInteger size = 0; // 获得文件夹中的所有内容 NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self]; for (NSString *subpath in enumerator) { // 获得全路径 NSString *fullSubpath = [self stringByAppendingPathComponent:subpath]; // 获得文件属性 size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize; } return size;} else { // 文件 return [mgr attributesOfItemAtPath:self error:nil].fileSize;}}
再多加点清除缓存的清除缓存一般包括SDWebImage的图片缓存,清理SDWebImage的缓存有自己的方法
[[SDImageCache sharedImageCache] cleanDisk];//清除磁盘上的过期缓存(默认保存周期是一周,一般不用这个) [[SDImageCache sharedImageCache] clearDisk];//清除磁盘上的所有缓存 [[SDImageCache sharedImageCache] clearMemory];//可有可无
清除文件的缓存(第三方的显示圈圈 这是封装到UITableViewCell里面的一个方法)下面是全部代码仅供参考
//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { XMGLog(@"%zd", XMGMyCachesFile.length); XMGLog(@"%zd", XMGCacheFile.length); self.textLabel.text = XMGDefaultText; // 禁止点击事件 self.userInteractionEnabled = NO; // 右边显示圈圈 UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [loadingView startAnimating]; self.accessoryView = loadingView; // 计算大小 [[[NSOperationQueue alloc] init] addOperationWithBlock:^{ // 计算缓存大小 NSInteger size = XMGCacheFile.fileSize; CGFloat unit = 1000.0; NSString *sizeText = nil; if (size >= unit * unit * unit) { // >= 1GB sizeText = [NSString stringWithFormat:@"%.1fGB", size / unit / unit / unit]; } else if (size >= unit * unit) { // >= 1MB sizeText = [NSString stringWithFormat:@"%.1fMB", size / unit / unit]; } else if (size >= unit) { // >= 1KB sizeText = [NSString stringWithFormat:@"%.1fKB", size / unit]; } else { // >= 0B sizeText = [NSString stringWithFormat:@"%zdB", size]; } NSString *text = [NSString stringWithFormat:@"%@(%@)", XMGDefaultText, sizeText]; // 回到主线程 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.textLabel.text = text; self.accessoryType = UITableViewCellAccessoryDisclosureIndicator; self.accessoryView = nil; // 允许点击事件 self.userInteractionEnabled = YES; }]; }];}return self;}//- (void)updateStatus{if (self.accessoryView == nil) return;// 让圈圈继续旋转UIActivityIndicatorView *loadingView = (UIActivityIndicatorView *)self.accessoryView;[loadingView startAnimating];}//- (void)clearCache{[SVProgressHUD showWithStatus:@"正在清除缓存" maskType:SVProgressHUDMaskTypeBlack];[[[NSOperationQueue alloc] init] addOperationWithBlock:^{ [[NSFileManager defaultManager] removeItemAtPath:XMGCacheFile error:nil]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [SVProgressHUD showSuccessWithStatus:@"清除成功"]; self.textLabel.text = XMGDefaultText; // 禁止点击事件 self.userInteractionEnabled = NO; }];}];}
0 0
- NSString 继续分类的介绍(清除缓存)
- 缓存分类介绍
- 过滤器的实现(清除缓存)
- 清除UIWebView的缓存
- 清除UIWebView的缓存
- 清除UIWebView的缓存
- 清除浏览器的缓存
- 清除UIWebView的缓存
- 清除ios的缓存
- 清除WKWebView的缓存
- 清除缓存的方法
- 清除input的缓存
- 109,NSString的介绍
- NSString 分类
- 清除浏览器里面的缓存
- 如何清除浏览器的缓存
- php 清除缓存的方法
- 如何清除浏览器的缓存
- Java-API文档的使用
- 37. Sudoku Solver
- 忽略
- MongoDB document转换为json _id为对象问题
- Android 知识总结,面试题
- NSString 继续分类的介绍(清除缓存)
- tomcat溢出
- 最简单最适用python 打包成exe
- FragmentTabhost解决IllegalStateException问题
- HTML5中地理位置
- 单词检索
- spring 学习(二) Profile
- nodejs 的群聊及私聊
- 生命游戏(Game of Life)