iOS-常用工具库代码
来源:互联网 发布:c语言null 编辑:程序博客网 时间:2024/06/07 16:33
//一.对文件和文件夹处理
//功能:获取文件夹大小,判断缓存是否过期,超出预设容量,
//备注:文件夹的名字是url通过MD5,Hash得到的字符串。
#pragma mark - Cache Helper- (NSString *)filePathFromRequest:(NSURLRequest *)request isInfo:(BOOL)info{ NSString *url = request.URL.absoluteString; NSString *fileName = [self cacheRequestFileName:url]; NSString *otherInfoFileName = [self cacheRequestOtherInfoFileName:url]; NSString *filePath = [self cacheFilePath:fileName]; NSString *fileInfoPath = [self cacheFilePath:otherInfoFileName]; if (info) { return fileInfoPath; } return filePath;}- (NSString *)cacheRequestFileName:(NSString *)requestUrl { return [self md5Hash:[NSString stringWithFormat:@"%@",requestUrl]];}- (NSString *)cacheRequestOtherInfoFileName:(NSString *)requestUrl { return [self md5Hash:[NSString stringWithFormat:@"%@-otherInfo",requestUrl]];}- (NSString *)cacheFilePath:(NSString *)file { NSString *path = [NSString stringWithFormat:@"%@/%@",self.diskPath,self.cacheFolder]; NSFileManager *fm = [NSFileManager defaultManager]; BOOL isDir; if ([fm fileExistsAtPath:path isDirectory:&isDir] && isDir) { // } else { [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; } NSString *subDirPath = [NSString stringWithFormat:@"%@/%@/%@",self.diskPath,self.cacheFolder,self.subDirectory]; if ([fm fileExistsAtPath:subDirPath isDirectory:&isDir] && isDir) { // } else { [fm createDirectoryAtPath:subDirPath withIntermediateDirectories:YES attributes:nil error:nil]; } NSString *cFilePath = [NSString stringWithFormat:@"%@/%@",subDirPath,file];// NSLog(@"%@",cFilePath); return cFilePath;}//清除自建的缓存目录- (void)checkCapacity { if ([self folderSize] > self.diskCapacity) { [self deleteCacheFolder]; }}- (void)deleteCacheFolder { [[NSFileManager defaultManager] removeItemAtPath:[self cacheFolderPath] error:nil];}- (NSUInteger)folderSize { NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:[self cacheFolderPath] error:nil]; NSEnumerator *filesEnumerator = [filesArray objectEnumerator]; NSString *fileName; unsigned long long int fileSize = 0; while (fileName = [filesEnumerator nextObject]) { NSDictionary *fileDic = [[NSFileManager defaultManager] attributesOfItemAtPath:[[self cacheFolderPath] stringByAppendingPathComponent:fileName] error:nil]; fileSize += [fileDic fileSize]; } return (NSUInteger)fileSize;}#pragma mark - Function Helper- (NSString *)md5Hash:(NSString *)str { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5( cStr, (CC_LONG)strlen(cStr), result ); NSString *md5Result = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ]; return md5Result;}- (NSString *)cacheFolderPath { return [NSString stringWithFormat:@"%@/%@/%@",self.diskPath,self.cacheFolder,self.subDirectory];}#pragma mark - Getter- (NSString *)diskPath { if (!_diskPath) { _diskPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; } return _diskPath;}- (NSMutableDictionary *)responseDic { if (!_responseDic) { _responseDic = [NSMutableDictionary dictionaryWithCapacity:0]; } return _responseDic;}- (NSString *)cacheFolder { if (!_cacheFolder) { _cacheFolder = @"Url"; } return _cacheFolder;}- (NSString *)subDirectory { if (!_subDirectory) { _subDirectory = @"UrlCacheDownload"; } return _subDirectory;}- (NSUInteger)memoryCapacity { if (!_memoryCapacity) { _memoryCapacity = 20 * 1024 * 1024; } return _memoryCapacity;}- (NSUInteger)diskCapacity { if (!_diskCapacity) { _diskCapacity = 200 * 1024 * 1024; } return _diskCapacity;}- (NSUInteger)cacheTime { if (!_cacheTime) { _cacheTime = 0; } return _cacheTime;}
0 0
- iOS-常用工具库代码
- 代码常用工具
- IOS常用工具
- IOS 常用工具代码汇总(持续更新中)
- iOS开发常用工具和第三方库
- java常用工具代码段
- Java代码常用工具--Draft
- Java 常用工具代码
- 常用工具代码总结
- 常用工具类代码段
- PH代码审计常用工具
- JS常用工具代码
- 前端代码常用工具类
- ios 常用工具类
- ios常用工具总结1
- ios 常用工具类
- IOS开发常用工具
- IOS开发常用工具网站
- div常用属性
- Android 即时音视频解决方案1——环信
- Java基础-IO流6 流的操作规律
- Kafka
- oracle数据库做定时任务(插入) 笔记
- iOS-常用工具库代码
- B. Spotlights
- 计算机网络(六) 网关--内部网关协议RIP和OSPF
- 图像缩放——双线性插值算法
- log4j配置
- 关于STM32 定时器PWM输出,关闭PWM时,输出引脚电平控制。
- 通过文件名利用NSClassFromString, 取到Class类别,通过setValue:forKey:传递值
- SurfaceView的介绍和使用
- Matlab之Treeplot