From 80d379609fa6631774634ab59cdf3f38f4a37e52 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Tue, 10 Apr 2018 16:57:25 -0400 Subject: [PATCH] update url cache method --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 25 ++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 43c28c1..6cde35a 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -528,18 +528,29 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; // 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 alloc] initWithMemoryCapacity:200*1024*1024 diskCapacity:200*1024*1024 diskPath:nil]; + NSURLCache *sharedCache = [NSURLCache sharedURLCache]; + [sharedCache setMemoryCapacity:200*1024*1024]; NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; configure.URLCache = sharedCache; NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1800]; - NSURLSessionDownloadTask *downloadImageTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { - NSLog(@"HEADDERRRSS %i for %@ %@", ((NSHTTPURLResponse *)response).statusCode,s7URL.absoluteString,((NSHTTPURLResponse *)response).allHeaderFields.description); - UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location] scale:[UIScreen mainScreen].scale]; + NSCachedURLResponse *response = [sharedCache cachedResponseForRequest:request]; + if (response != nil) { + UIImage *image = [UIImage imageWithData:response.data scale:[UIScreen mainScreen].scale]; completionHandler(image, nil, isFallBackImage); - }]; - [downloadImageTask resume]; - return; + return; + } else { + 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]; + [sharedCache storeCachedResponse:cachedreponse forRequest:request]; + NSLog(@"HEADDERRRSS %i for %@ %@", ((NSHTTPURLResponse *)response).statusCode,s7URL.absoluteString,((NSHTTPURLResponse *)response).allHeaderFields.description); + UIImage *image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale]; + completionHandler(image, nil, isFallBackImage); + }]; + [downloadImageTask resume]; + return; + } /* // Check in memory cache img = [self getCachedImageWithName:s7URL.absoluteString];