前言
客户反馈查看图片的速度非常慢,于是我们需要做一下图片的缓存,由于图片都是后台base64传过来的,所以,需要存在本地,无法使用SDWebImage来进行优化。
原始做法
1.保存图片,使用NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
生成的路径+图片名 作为key,保存图片。
#pragma mark - 保存图片
-(void)saveImages:(NSArray *)arr
{
NSString *filePath = [self generateFilePath];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
[arr enumerateObjectsUsingBlock:^(GetBussinessRecivingImagePage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *imgPath = [filePath stringByAppendingString:obj.ImageName];
NSError *error = nil;
BOOL result = [UIImagePNGRepresentation(obj.image) writeToFile:imgPath options:NSDataWritingAtomic error:&error];
if (!result) {
NSLog(@"\n%@", imgPath);
}
}];
}
#pragma mark - 生成路径
-(NSString *)generateFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingString:[NSString stringWithFormat:@"/ScanningCopy/%@/%@/",self.xxx, self.xxx]];
return filePath;
}
- 获取图片,使用生成的路径+文件名作为key,来获取图片。
#pragma mark - 获取图片
-(NSArray *)getImages
{
NSString *filePath = [self generateFilePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *files = [fm subpathsAtPath:filePath];
NSMutableArray *images = [NSMutableArray array];
[files enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIImage *img = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",filePath,obj]];
[images addObject:img];
}];
return images;
}
- 删除图片,使用 路径+图片名 作为key来删除图片。
#pragma mark - 删除图片
-(void)deleateImages
{
NSString *filePath = [self generateFilePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *files = [fm subpathsAtPath:filePath];
[files enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSError *error = nil;
BOOL res = [fm removeItemAtPath:[NSString stringWithFormat:@"%@/%@",filePath,obj] error:&error];
if (!res) {
NSLog(@"%@", obj);
}
}];
}
- 当图片超过1G的时候,进行清理,清理的顺序依次是10天前,5天前,3天前和所有。
#pragma mark - 计算现有图片占据的大小
-(void)calculateCacheSize
{
float maxsize = 1024;
NSString *cachePath = [self generateFilePath];
float size = [self folderSizeAtPath:cachePath];
if (size > maxsize) {
NSDate *tenDaysBefore = [[NSDate date] initWithTimeIntervalSinceNow:24*60*60*(-10)];
[self cleanFolder:cachePath before:tenDaysBefore];
}
size = [self folderSizeAtPath:cachePath];
if (size > maxsize) {
NSDate *fiveDaysBefore = [[NSDate date] initWithTimeIntervalSinceNow:24*60*60*(-5)];
[self cleanFolder:cachePath before:fiveDaysBefore];
}
size = [self folderSizeAtPath:cachePath];
if (size > maxsize) {
NSDate *fiveDaysBefore = [[NSDate date] initWithTimeIntervalSinceNow:24*60*60*(-3)];
[self cleanFolder:cachePath before:fiveDaysBefore];
}
size = [self folderSizeAtPath:cachePath];
if (size > maxsize) {
NSDate *nowDate = [NSDate date];
[self cleanFolder:cachePath before:nowDate];
}
}
#pragma mark - 清除beforeDate之前的图片
-(void)cleanFolder:(NSString *)folderPath before:(NSDate *)beforeDate
{
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:folderPath]) return;
NSEnumerator *childFilesEnumerator = [[fm subpathsAtPath:folderPath] objectEnumerator];
NSString *fileName;
while ((fileName = [childFilesEnumerator nextObject]) != nil) {
NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
NSError *error = nil;
NSDictionary *fileAttributes = [fm attributesOfItemAtPath:fileAbsolutePath error:&error];
NSDate *creationDate = [fileAttributes objectForKey:NSFileCreationDate];
//创建时间跟需要删除的时间比较,若创建时间在指定要删除的日期前面,则删除
if ([beforeDate compare:creationDate] == NSOrderedDescending) {
[fm removeItemAtPath:fileAbsolutePath error:&error];
}
}
}
#pragma mark - 生成路径
-(NSString *)generateFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingString:[NSString stringWithFormat:@"/xxx"]];
return filePath;
}
#pragma mark - 单个文件的大小
-(long long)fileSizeAtPath:(NSString *)filePath
{
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]) {
NSError *error = nil;
return [[manager attributesOfItemAtPath:filePath error:&error] fileSize];
}
return 0;
}
#pragma mark - 遍历文件夹来获得文件夹大小,返回多少M
-(float)folderSizeAtPath:(NSString *)folderPath
{
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:folderPath]) return 0;
NSEnumerator *childFilesEnumerator = [[fm subpathsAtPath:folderPath] objectEnumerator];
NSString *fileName;
long long folderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil) {
NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize/(1024.0*1024.0);
}
使用PINCache优化
三句代码替换了上面这一堆。
//保存图片
[[PINCache sharedCache] setObject:self.imgArr forKey:self.pguid];
//磁盘使用最大开销限制为1G
[[PINCache sharedCache].diskCache setByteLimit:1024*1024*1024];
//取图片
NSArray *localImages = [[PINCache sharedCache] objectForKey:self.pguid];
网友评论