update url cache method

This commit is contained in:
Pan, Xinlei (Ryan) 2018-04-10 16:57:25 -04:00
parent e4e617581c
commit 80d379609f

View File

@ -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];