check fall back image for s7 image

This commit is contained in:
Pan, Xinlei (Ryan) 2018-04-20 14:59:56 -04:00
parent 2ca6c854d8
commit 6700d97b81

View File

@ -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 {