iOS 获取缓存大小与清除缓存
2017-10-16 11:24
405 查看
首先要获取cache文件夹路径
注意:返回的单位是(B),下面是单位换算
#define cachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
// // LLFileTool.h // BuDeJie // // Created by llkj on 2017/10/13. // Copyright © 2017年 LayneCheung. All rights reserved. // 处理文件缓存 #import <Foundation/Foundation.h> @interface LLFileTool : NSObject /** 删除文件夹所有文件 @param directoryPath 文件夹路径 */ + (void)removeDirectoryPath:(NSString *)directoryPath; /** 获取文件夹尺寸 @param directoryPath 文件夹路径 @param completion 文件夹尺寸 */ + (void)getFileSize:(NSString *)directoryPath completion:(void(^)(NSInteger totalSize))completion; @end
// // LLFileTool.m // BuDeJie // // Created by llkj on 2017/10/13. // Copyright © 2017年 LayneCheung. All rights reserved. // #import "LLFileTool.h" @implementation LLFileTool + (void)removeDirectoryPath:(NSString *)directoryPath { //获取文件管理者 NSFileManager *mgr = [NSFileManager defaultManager]; BOOL isDirectoey; BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectoey]; if (!isExist || !isDirectoey) { NSException *exception = [NSException exceptionWithName:@"PathError" reason:@"需要传入的是文件夹路径,并且路径要存在!" userInfo:nil]; [exception raise]; } //获取cache文件夹下所有文件,不包括子路径的子路径 NSArray *subPaths = [mgr contentsOfDirectoryAtPath:directoryPath error:nil]; for (NSString *subPath in subPaths) { //拼接完整路径 NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath]; //删除路径 [mgr removeItemAtPath:filePath error:nil]; } } + (void)getFileSize:(NSString *)directoryPath completion:(void(^)(NSInteger totalSize))completion { NSFileManager *mgr =[NSFileManager defaultManager]; BOOL isDirectory; BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory]; if (!isDirectory || !isExist) { NSException *exception = [NSException exceptionWithName:@"PathError" reason:@"需要传入的是文件夹路径,并且路径要存在!" userInfo:nil]; [exception raise]; } dispatch_async(dispatch_get_global_queue(0, 0), ^{ //获取文件夹下所有文件,包括子路径的子路径 NSArray *subPaths = [mgr subpathsAtPath:directoryPath]; NSInteger totalSize = 0; for (NSString *subPath in subPaths) { //获取文件全路径 NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath]; //判断隐藏文件 if ([filePath containsString:@".DS"]) continue; //判断是否文件夹 BOOL isDircetory; //判断文件是否存在,并判断是否是文件夹 BOOL isExist = [mgr fileExistsAtPath:filePath isDirectory:&isDircetory]; if (isDircetory || !isExist) continue; //获取文件属性 NSDictionary *attr = [mgr attributesOfItemAtPath:filePath error:nil]; NSInteger size = [attr fileSize]; totalSize += size; } //计算完成回调 dispatch_sync(dispatch_get_main_queue(), ^{ if (completion) { completion(totalSize); } }); }); } @end
注意:返回的单位是(B),下面是单位换算
- (NSString *)cacheSizeStr { NSInteger totalSize = _totalSize; NSString *sizeStr = @"清除缓存"; if (totalSize > 1000 * 1000) { CGFloat sizeF = totalSize / 1000.0 / 1000.0; sizeStr = [NSString stringWithFormat:@"%@(%.1fMB)", sizeStr, sizeF]; } else if (totalSize > 1000) { CGFloat sizeF = totalSize / 1000.0; sizeStr 4000 = [NSString stringWithFormat:@"%@(%.1fKB)", sizeStr, sizeF]; } else if (totalSize > 0) { sizeStr = [NSString stringWithFormat:@"%@(%.ldB)", sizeStr, totalSize]; } return sizeStr; }
相关文章推荐
- iOS中显示获取缓存的大小多少M,点击清除缓存方法
- iOS中显示获取缓存的大小多少M,点击清除缓存方法
- 【iOS】获取缓存大小清除缓存的方法
- IOS获取缓存文件的大小并清除缓存文件的方法
- iOS 获取缓存大小 清除缓存
- iOS获取缓存文件的大小并清除缓存
- iOS中显示获取缓存的大小多少M,点击清除缓存方法 (2016-03-03 12:12:01)
- ios 缓存大小, 清除缓存 成 封装工具
- Glide 缓存工具例子,缓存大小获取,磁盘缓存清除(2 种方法),内存缓存清除
- iOS计算缓存大小、清除缓存
- Android获取缓存大小和清除缓存功能
- Android记录20-获取缓存大小和清除缓存功能
- iOS 缓存的获取计算与清除归零
- 获取document路径以及其下面的文件大小 & 清除文件,缓存
- Android中Glide获取缓存大小并清除缓存图片
- Android开发记录20-获取缓存大小和清除缓存功能
- iOS 获取应用程序缓存大小
- Glide获取缓存大小并清除缓存图片
- Android记录20-获取缓存大小和清除缓存功能
- Glide获取缓存大小并清除缓存图片