// // YMAdImageTool.m // YUMI // // Created by YUMI on 2022/10/31. // #import "XPAdImageTool.h" #import #import "UploadFile.h" #define CACHENAME @"XPUserCache" UIKIT_EXTERN NSString * const adImageName; @interface XPAdImageTool () @property (nonatomic, strong) YYCache *yyCache; ///广告信息 @property (nonatomic,strong) AdvertiseModel *infoModel; @end static XPAdImageTool* tool; @implementation XPAdImageTool + (instancetype)shareImageTool { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ tool = [[XPAdImageTool alloc] init]; }); return tool; } - (instancetype)init { self = [super init]; if (self) { self.yyCache = [YYCache cacheWithName:CACHENAME]; } return self; } //去除广告信息 - (AdvertiseModel *)getAdInfoFromCacheInMainWith:(NSString *)link { if (link.length > 0) { if ([self.yyCache containsObjectForKey:link]) { return (AdvertiseModel *)[self.yyCache objectForKey:link]; }else { return nil; } }else { return nil; } return nil; } ///保存信息 - (void)saveAdInfo:(AdvertiseModel *)adInfo { self.infoModel = adInfo; NSArray *stringArr = [adInfo.pict componentsSeparatedByString:@"/"]; NSString *key = stringArr.lastObject; [self.yyCache setObject:(id )adInfo forKey:key withBlock:^{ }]; [self getAdvertisingImage]; } /** * 判断文件是否存在 */ - (BOOL)isFileExistWithFilePath:(NSString *)filePath { NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isDirectory = FALSE; return [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory]; } /** * 初始化广告页面 */ - (void)getAdvertisingImage { if (self.infoModel.pict.length > 0) { NSString *imageUrl = self.infoModel.pict; // 获取图片名 NSArray *stringArr = [imageUrl componentsSeparatedByString:@"/"]; NSString *imageName = stringArr.lastObject; // 拼接沙盒路径 NSString *filePath = [self getFilePathWithImageName:imageName]; BOOL isExist = [self isFileExistWithFilePath:filePath]; if (!isExist){// 如果该图片不存在,则删除老图片,下载新图片 [self downloadAdImageWithUrl:imageUrl imageName:imageName]; } }else { [self deleteOldImage]; } } /** * 下载新图片 */ - (void)downloadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSString *filePath = [self getFilePathWithImageName:imageName]; // 保存文件的名称 if ([imageUrl.lowercaseString hasSuffix:@"svga"]) { @kWeakify(self); [[UploadFile share] download:imageUrl path:filePath complete:^{ @kStrongify(self); [self deleteOldImage]; [[NSUserDefaults standardUserDefaults] setValue:imageName forKey:adImageName]; [[NSUserDefaults standardUserDefaults] synchronize]; } failure:^{ @kStrongify(self); [self deleteOldImage]; }]; } else { NSString *encode = [imageUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:encode]]; UIImage *image = [UIImage imageWithData:data]; if ([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]) {// 保存成功 [self deleteOldImage]; [[NSUserDefaults standardUserDefaults] setValue:imageName forKey:adImageName]; [[NSUserDefaults standardUserDefaults] synchronize]; }else{ [self deleteOldImage]; } } }); } /** * 删除旧图片 */ - (void)deleteOldImage { NSString *imageName = [[NSUserDefaults standardUserDefaults] valueForKey:adImageName]; if (imageName) { NSString *filePath = [self getFilePathWithImageName:imageName]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:filePath error:nil]; } } /** * 根据图片名拼接文件路径 */ - (NSString *)getFilePathWithImageName:(NSString *)imageName { if (imageName) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES); NSString *filePath = [[paths xpSafeObjectAtIndex:0] stringByAppendingPathComponent:imageName]; return filePath; } return nil; } @end