add cache method to gif image

This commit is contained in:
Pan, Xinlei (Ryan) 2018-04-20 15:25:33 -04:00
parent 6700d97b81
commit 7904d4e8fe

View File

@ -457,31 +457,17 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
- (void)getGif:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL isFallBackImage = NO;
NSData *imageData = nil;
if (pathOrName.length > 0) {
if([pathOrName containsString:@"http"]) {
if(pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) {
// Gets the full path
NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:CGRectNull flipImage:NO];
if (s7URL) {
// Check in memory cache
imageData = [self getCachedDataWithName:s7URL.absoluteString];
if (!imageData) {
imageData = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL];
if (imageData) {
[self addDataToCache:imageData withName:s7URL.absoluteString];
}
}
[self downloadImage:s7URL isGif:YES fallbackImageName:fallbackImageName completionHandler:completionHandler];
}
} else {
// Try Local. Check in memory cache
imageData = [self getCachedDataWithName:pathOrName];
if (!imageData) {
imageData = [NSData dataWithContentsOfFile:[[self bundleToUseForImages] pathForResource:pathOrName ofType:@"gif"]];
if (imageData) {
@ -490,25 +476,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
}
}
}
// Set to the fallback image.
UIImage *fallbackImage = nil;
if (!imageData && fallbackImageName) {
isFallBackImage = YES;
// Grab fallback from cache
fallbackImage = [self getCachedImageWithName:fallbackImageName];
if (!fallbackImage) {
fallbackImage = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil];
if (fallbackImage) {
// Cache the fallback image if not already cached.
[self addImageToCache:fallbackImage withName:fallbackImageName];
}
}
}
completionHandler(fallbackImage, imageData, isFallBackImage);
[self checkImage:nil imageData:imageData fallbackImage:fallbackImageName completionHandler:completionHandler];
});
}
@ -521,48 +489,17 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *img = nil;
if (pathOrName.length > 0) {
// try the url if it's a url.
if (pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) {
// Gets the full path
NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:finalRect flipImage:flip];
if (s7URL) {
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration];
configure.URLCache = sharedCache;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configure];
NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut];
NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request];
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];
//check fallback image
[self checkImage:image fallbackImage:fallbackImageName completionHandler:completionHandler];
}];
[downloadImageTask resume];
[self downloadImage:s7URL isGif:NO fallbackImageName:fallbackImageName completionHandler:completionHandler];
return;
}
} else {
// Try native image. Check in memory cache
img = [self getCachedImageWithName:pathOrName];
if (!img) {
img = [UIImage imageNamed:pathOrName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil];
@ -572,15 +509,48 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
}
}
}
[self checkImage:img fallbackImage:fallbackImageName completionHandler:completionHandler];
[self checkImage:img imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler];
});
}
- (void)checkImage:(UIImage *)img fallbackImage:(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];
NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration];
configure.URLCache = sharedCache;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configure];
NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut];
NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request];
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];
if (isGif) {
[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];
}
}];
[downloadImageTask resume];
}
- (void)checkImage:(UIImage *)img imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{
// Set to the fallback image.
BOOL isFallBackImage = NO;
if (!img && fallbackImageName) {
if (!img && !imageData && fallbackImageName) {
isFallBackImage = YES;
// Grab fallback from cache
img = [self getCachedImageWithName:fallbackImageName];