From 6700d97b81fc95c8dea61e64dd977affef4dad02 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Fri, 20 Apr 2018 14:59:56 -0400 Subject: [PATCH] check fall back image for s7 image --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 43 ++++++++++++-------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 8fee473..3e84a33 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -519,7 +519,6 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; - (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format finalRect:(CGRect)finalRect flipImage:(BOOL)flip localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - BOOL isFallBackImage = NO; UIImage *img = nil; if (pathOrName.length > 0) { @@ -539,15 +538,22 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; if (!cachedResponse.data) { cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL]; if (cachedResponse) { + //system stores cache based datatask and check cache based on response, + //need to manually store cache for response [sharedCache storeCachedResponse:cachedResponse forRequest:request]; } } NSURLSessionDownloadTask *downloadImageTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSData *data = [NSData dataWithContentsOfURL:location]; NSCachedURLResponse *cachedreponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; + //system stores cache based datatask and check cache based on response, + //need to manually store cache for response [sharedCache storeCachedResponse:cachedreponse forRequest:request]; UIImage *image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale]; - completionHandler(image, nil, isFallBackImage); + + //check fallback image + [self checkImage:image fallbackImage:fallbackImageName completionHandler:completionHandler]; + }]; [downloadImageTask resume]; return; @@ -566,24 +572,29 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } } } + [self checkImage:img fallbackImage:fallbackImageName completionHandler:completionHandler]; - // Set to the fallback image. - if (!img && fallbackImageName) { - isFallBackImage = YES; - // Grab fallback from cache - img = [self getCachedImageWithName:fallbackImageName]; - if (!img) { + }); +} + +- (void)checkImage:(UIImage *)img fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ + // Set to the fallback image. + BOOL isFallBackImage = NO; + if (!img && fallbackImageName) { + isFallBackImage = YES; + // Grab fallback from cache + img = [self getCachedImageWithName:fallbackImageName]; + if (!img) { + + img = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; + if (img) { - img = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; - if (img) { - - // Cache the fallback image if not already cached. - [self addImageToCache:img withName:fallbackImageName]; - } + // Cache the fallback image if not already cached. + [self addImageToCache:img withName:fallbackImageName]; } } - completionHandler(img, nil, isFallBackImage); - }); + } + completionHandler(img, nil, isFallBackImage); } - (nullable UIImage *)getCachedImageWithName:(nonnull NSString *)imageName {