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
原创粉丝点击