|
|
@@ -19,7 +19,6 @@ static YRLoadImgCacheTool *instance = nil;
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
|
instance = [YRLoadImgCacheTool new];
|
|
|
-
|
|
|
|
|
|
});
|
|
|
|
|
|
@@ -27,8 +26,396 @@ static YRLoadImgCacheTool *instance = nil;
|
|
|
|
|
|
}
|
|
|
|
|
|
--(void)getAppLoadImage{}
|
|
|
|
|
|
+#pragma mark - app启动图管理
|
|
|
+-(void)getAppLoadImage{
|
|
|
+
|
|
|
+
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
+
|
|
|
+ [self configSdWebImage];
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
+
|
|
|
+ // http://113.105.74.140:8092/user/appStart
|
|
|
+ // http://192.168.253.36/men.json
|
|
|
+ NSString *urlStr = [NSString stringWithFormat:@"http://113.105.74.140:8092/user/appStart"];
|
|
|
+
|
|
|
+ NSDictionary *dic = @{@"token":@(1)};
|
|
|
+
|
|
|
+ [[YRNetworkManager shareManager] request:urlStr method:GET parameters:dic progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
|
|
|
+
|
|
|
+ @try{//防止刚启动出现异常
|
|
|
+
|
|
|
+ NSDictionary *resDic = responseObject;
|
|
|
+
|
|
|
+ if ([responseObject isKindOfClass:[NSData class]]) {
|
|
|
+
|
|
|
+ resDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ NSArray *arr = resDic[@"result"];
|
|
|
+
|
|
|
+ if (![arr isKindOfClass:[NSArray class]]) {
|
|
|
+
|
|
|
+ arr = [NSArray new];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (arr.count > 0) {
|
|
|
+
|
|
|
+ NSDictionary *dic = arr[0];
|
|
|
+
|
|
|
+ NSString *urlStrOne = dic[@"aa_urlc"];
|
|
|
+
|
|
|
+ if ([urlStrOne isKindOfClass:[NSString class]]) {
|
|
|
+
|
|
|
+ if ([urlStrOne containsString:@"http:"]) {
|
|
|
+
|
|
|
+ NSString *theLast = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"theLastAppLoadImagesUrl"]];
|
|
|
+
|
|
|
+ if(![theLast isEqualToString:urlStrOne]){//如果最后的链接是不一样的,更新本地图片数据
|
|
|
+
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
+
|
|
|
+ [self clearImageCache];
|
|
|
+
|
|
|
+ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStrOne]];
|
|
|
+
|
|
|
+ if(![urlStrOne containsString:@".gif"]){//单张图片
|
|
|
+
|
|
|
+ UIImage *image = [UIImage imageWithData:data];
|
|
|
+
|
|
|
+ if (image){
|
|
|
+
|
|
|
+ NSString *filePath = [self getFilePath];
|
|
|
+
|
|
|
+ if ([[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]) {
|
|
|
+
|
|
|
+ NSString *str = [NSString stringWithFormat:@"loadingAd.png"];
|
|
|
+
|
|
|
+ filePath = [filePath stringByAppendingPathComponent:str];
|
|
|
+
|
|
|
+ if([[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil]){
|
|
|
+
|
|
|
+ [[NSUserDefaults standardUserDefaults] setObject:urlStrOne forKey:@"theLastAppLoadImagesUrl"];
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{//gif图片
|
|
|
+
|
|
|
+ [self cacheGifImageWithUrl:urlStrOne];
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }else{//如果链接是一样的并且是gif图片,则查看本地的数据是否是对的
|
|
|
+
|
|
|
+ if([urlStrOne containsString:@".gif"]){
|
|
|
+
|
|
|
+ NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",urlStrOne]];
|
|
|
+ CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
|
|
|
+ size_t gifCount = CGImageSourceGetCount(gifSource);
|
|
|
+ NSInteger count = [self getCacheAppImages].count;
|
|
|
+
|
|
|
+ if(gifCount != count){//如果数量不一致,则存储可能出错
|
|
|
+
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
|
|
|
+ [self clearImageCache];
|
|
|
+
|
|
|
+ [self cacheGifImageWithUrl:urlStrOne];
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [self clearImageCache];
|
|
|
+ [[NSUserDefaults standardUserDefaults] setObject:@"noImage" forKey:@"theLastAppLoadImagesUrl"];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } @catch (NSException *exception) {//出现异常,清数据
|
|
|
+
|
|
|
+
|
|
|
+ [self clearImageCache];
|
|
|
+ [[NSUserDefaults standardUserDefaults] setObject:@"noImage" forKey:@"theLastAppLoadImagesUrl"];
|
|
|
+ instance = nil;
|
|
|
+
|
|
|
+ } @finally {
|
|
|
+
|
|
|
+ instance = nil;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } failure:^(NSURLSessionDataTask *task, NSError *error) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+-(void)cacheGifImageWithUrl:(NSString *)url{
|
|
|
+
|
|
|
+ [self clearImageCache];
|
|
|
+
|
|
|
+ if (url == nil) {
|
|
|
+
|
|
|
+ url = @"";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]];
|
|
|
+ CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
|
|
|
+ size_t gifCount = CGImageSourceGetCount(gifSource);
|
|
|
+ NSMutableArray *imageArr = [[NSMutableArray alloc]init];
|
|
|
+
|
|
|
+ BOOL result = YES;
|
|
|
+
|
|
|
+ for (int i = 0; i< gifCount; i++) {
|
|
|
+
|
|
|
+ CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
|
|
|
+ UIImage *image = [UIImage imageWithCGImage:imageRef];
|
|
|
+
|
|
|
+ if (image != nil) {
|
|
|
+
|
|
|
+ [imageArr addObject:image];
|
|
|
+
|
|
|
+ if(![self cacheImageWithImage:image index:i]){
|
|
|
+
|
|
|
+ result = NO;
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ CGImageRelease(imageRef);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(result){//存储成功
|
|
|
+
|
|
|
+ [[NSUserDefaults standardUserDefaults] setObject:url forKey:@"theLastAppLoadImagesUrl"];
|
|
|
+
|
|
|
+ }else{//某张图片存储发生错误,全部清除
|
|
|
+
|
|
|
+ [self clearImageCache];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+-(BOOL)cacheImageWithImage:(UIImage *)image index:(int)index{
|
|
|
+
|
|
|
+
|
|
|
+ NSData *data = UIImagePNGRepresentation(image);
|
|
|
+
|
|
|
+ NSString *str = [NSString stringWithFormat:@"loadingAd%d.png",index];
|
|
|
+
|
|
|
+ NSString *filePath = [self getFilePath];
|
|
|
+
|
|
|
+ filePath = [filePath stringByAppendingPathComponent:str];
|
|
|
+
|
|
|
+ return [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+-(void)clearImageCache{
|
|
|
+
|
|
|
+
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
+
|
|
|
+ NSString *filePath = [self getFilePath];
|
|
|
+
|
|
|
+ NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
+
|
|
|
+ NSArray *fileArr = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtPath:filePath error:nil]];
|
|
|
+
|
|
|
+ for (NSString *file in fileArr) {
|
|
|
+
|
|
|
+ NSString *fileP = [filePath stringByAppendingPathComponent:file];
|
|
|
+
|
|
|
+ [[NSFileManager defaultManager] removeItemAtPath:fileP error:nil];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+-(NSMutableArray<UIImage *> *)getCacheAppImages{
|
|
|
+
|
|
|
+ NSMutableArray *imgArr = [NSMutableArray new];
|
|
|
+
|
|
|
+ NSString *filePath = [self getFilePath];
|
|
|
+
|
|
|
+ NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
+
|
|
|
+ NSArray<NSString *> *fileAr = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtPath:filePath error:nil]];
|
|
|
+
|
|
|
+ NSMutableArray<NSString *> *fileArr = [fileAr mutableCopy];
|
|
|
+
|
|
|
+ if(fileArr.count== 0){
|
|
|
+
|
|
|
+ return imgArr;
|
|
|
+
|
|
|
+ }else if(fileArr.count==1){
|
|
|
+
|
|
|
+ if([fileArr[0] containsString:@".png"]){
|
|
|
+
|
|
|
+ NSString *imagePath = [filePath stringByAppendingPathComponent:fileArr[0]];
|
|
|
+
|
|
|
+ UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
|
|
|
+
|
|
|
+ if(image && [image isKindOfClass:[UIImage class]]){
|
|
|
+
|
|
|
+ [imgArr addObject:image];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return imgArr;
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ for(int i=0;i<fileArr.count;i++){
|
|
|
+
|
|
|
+ for(int j=0;j<fileArr.count-i-1;j++){
|
|
|
+
|
|
|
+ NSString *fileNam = fileArr[j];
|
|
|
+ NSString *fileName = fileArr[j+1];
|
|
|
+
|
|
|
+ fileNam = [fileNam stringByReplacingOccurrencesOfString:@"loadingAd" withString:@""];
|
|
|
+ fileNam = [fileNam stringByReplacingOccurrencesOfString:@".png" withString:@""];
|
|
|
+ NSInteger fileI = [fileNam integerValue];
|
|
|
+ fileName = [fileName stringByReplacingOccurrencesOfString:@"loadingAd" withString:@""];
|
|
|
+ fileName = [fileName stringByReplacingOccurrencesOfString:@".png" withString:@""];
|
|
|
+ NSInteger fileIn = [fileName integerValue];
|
|
|
+ if(fileI > fileIn){
|
|
|
+
|
|
|
+ id temp = fileArr[j];
|
|
|
+ fileArr[j] = fileArr[j+1];
|
|
|
+ fileArr[j+1] = temp;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i= 0; i<fileArr.count; i++) {
|
|
|
+
|
|
|
+ NSString *filePath = [[self getFilePath] stringByAppendingPathComponent:fileArr[i]];
|
|
|
+
|
|
|
+ UIImage *image = [UIImage imageWithContentsOfFile:filePath];
|
|
|
+
|
|
|
+ if(image && [image isKindOfClass:[UIImage class]]){
|
|
|
+
|
|
|
+ [imgArr addObject:image];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return imgArr;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+-(NSString *)getFilePath{
|
|
|
+
|
|
|
+ NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
|
|
|
+
|
|
|
+ filePath = [filePath stringByAppendingPathComponent:@"appLoad"];
|
|
|
+
|
|
|
+ [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
+
|
|
|
+ return filePath;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+-(void)configSdWebImage{
|
|
|
+
|
|
|
+
|
|
|
+ SDWebImageDownloader *imgDownloader = SDWebImageManager.sharedManager.imageDownloader;
|
|
|
+
|
|
|
+ imgDownloader.headersFilter = ^NSDictionary *(NSURL *url, NSDictionary *headers) {
|
|
|
+
|
|
|
+ NSFileManager *fm = [[NSFileManager alloc] init];
|
|
|
+ NSString *imgKey = [SDWebImageManager.sharedManager cacheKeyForURL:url];
|
|
|
+ NSString *imgPath = [SDWebImageManager.sharedManager.imageCache defaultCachePathForKey:imgKey];
|
|
|
+ NSDictionary *fileAttr = [fm attributesOfItemAtPath:imgPath error:nil];
|
|
|
+
|
|
|
+ NSMutableDictionary *mutableHeaders = [headers mutableCopy];
|
|
|
+
|
|
|
+ NSDate *lastModifiedDate = nil;
|
|
|
+
|
|
|
+
|
|
|
+ if (fileAttr.count > 0) {
|
|
|
+
|
|
|
+ lastModifiedDate = (NSDate *)fileAttr[NSFileModificationDate];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
|
|
+ formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
|
|
|
+ formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
|
|
|
+ formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z";
|
|
|
+
|
|
|
+ NSString *lastModifiedStr = [formatter stringFromDate:lastModifiedDate];
|
|
|
+ lastModifiedStr = lastModifiedStr.length > 0 ? lastModifiedStr : @"";
|
|
|
+ [mutableHeaders setValue:lastModifiedStr forKey:@"If-Modified-Since"];
|
|
|
+
|
|
|
+ return mutableHeaders;
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+-(void)dealloc{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
@end
|