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