variable name change. cache tied from session

This commit is contained in:
Pfeil, Scott Robert 2018-05-10 12:50:46 -04:00
parent 01f4349a6d
commit 6edf0ab99c

View File

@ -488,7 +488,7 @@ 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), ^{
UIImage *img = nil;
UIImage *image = nil;
if (pathOrName.length > 0) {
// try the url if it's a url.
if (pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) {
@ -500,27 +500,27 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
}
} else {
// Try native image. Check in memory cache
img = [self getCachedImageWithName:pathOrName];
if (!img) {
img = [UIImage imageNamed:pathOrName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil];
if (img) {
[self addImageToCache:img withName:pathOrName];
image = [self getCachedImageWithName:pathOrName];
if (!image) {
image = [UIImage imageNamed:pathOrName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil];
if (image) {
[self addImageToCache:image withName:pathOrName];
}
}
}
}
[self checkImage:img imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler];
[self checkImage:image imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler];
});
}
//download image
- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
NSURLSession *session = [MVMCoreSessionObject sharedGlobal].session;
NSURLCache *sharedCache = session.configuration.URLCache;
NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut];
//use Freebee to download image first, then store image to cache
//use Freebee to download image first if it is not already in the cache, then store image to cache
NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request];
if (!cachedResponse.data) {
cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL];
@ -541,7 +541,6 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
[self checkImage:nil imageData:data fallbackImage:fallbackImageName completionHandler:completionHandler];
} else {
UIImage *image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale];
//check fallback image
[self checkImage:image imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler];
}
}];
@ -549,24 +548,24 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
}
//check whether get image or imageData, if not, return fallbackImage
- (void)checkImage:(UIImage *)img imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{
- (void)checkImage:(UIImage *)image imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
// Set to the fallback image.
BOOL isFallBackImage = NO;
if (!img && !imageData && fallbackImageName) {
if (!image && !imageData && fallbackImageName) {
isFallBackImage = YES;
// Grab fallback from cache
img = [self getCachedImageWithName:fallbackImageName];
if (!img) {
image = [self getCachedImageWithName:fallbackImageName];
if (!image) {
img = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil];
if (img) {
image = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil];
if (image) {
// Cache the fallback image if not already cached.
[self addImageToCache:img withName:fallbackImageName];
[self addImageToCache:image withName:fallbackImageName];
}
}
}
completionHandler(img, imageData, isFallBackImage);
completionHandler(image, imageData, isFallBackImage);
}
- (nullable UIImage *)getCachedImageWithName:(nonnull NSString *)imageName {