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

iOS 获取缓存大小与清除缓存

2017-10-16 11:24 405 查看
首先要获取cache文件夹路径

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息