From 647b0c926749151bfd4e0349e9e5ed0d7a853f2b Mon Sep 17 00:00:00 2001 From: Leland Richardson Date: Wed, 2 Nov 2016 10:30:40 -0700 Subject: [PATCH] [Airbnb] Fix Image Cache issues --- Libraries/Image/RCTImageCache.m | 7 +++---- Libraries/Image/RCTImageLoader.m | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Libraries/Image/RCTImageCache.m b/Libraries/Image/RCTImageCache.m index 267edd2da1c371..bdee1ef1c5907c 100644 --- a/Libraries/Image/RCTImageCache.m +++ b/Libraries/Image/RCTImageCache.m @@ -19,13 +19,12 @@ #import "RCTImageUtils.h" -static const NSUInteger RCTMaxCachableDecodedImageSizeInBytes = 1048576; // 1MB +static const NSUInteger RCTMaxCachableDecodedImageSizeInBytes = 1048576 * 4; // 4MB static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat scale, RCTResizeMode resizeMode, NSString *responseDate) { - return [NSString stringWithFormat:@"%@|%g|%g|%g|%zd|%@", - imageTag, size.width, size.height, scale, resizeMode, responseDate]; + return [NSString stringWithFormat:@"%@", imageTag]; } @implementation RCTImageCache @@ -37,7 +36,7 @@ @implementation RCTImageCache - (instancetype)init { _decodedImageCache = [NSCache new]; - _decodedImageCache.totalCostLimit = 5 * 1024 * 1024; // 5MB + _decodedImageCache.totalCostLimit = 32 * 1024 * 1024; // 32MB [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearCache) diff --git a/Libraries/Image/RCTImageLoader.m b/Libraries/Image/RCTImageLoader.m index b8edc9148a980c..eefb2206f6b6cd 100644 --- a/Libraries/Image/RCTImageLoader.m +++ b/Libraries/Image/RCTImageLoader.m @@ -386,6 +386,9 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest } else { // Use networking module to load image cancelLoad = [strongSelf _loadURLRequest:request + size:size + scale:scale + resizeMode:resizeMode progressBlock:progressHandler completionBlock:completionHandler]; } @@ -402,6 +405,9 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest } - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request + size:(CGSize)size + scale:(CGFloat)scale + resizeMode:(RCTResizeMode)resizeMode progressBlock:(RCTImageLoaderProgressBlock)progressHandler completionBlock:(void (^)(NSError *error, id imageOrData, NSString *fetchDate))completionHandler { @@ -413,6 +419,17 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request return NULL; } + UIImage *image = [[self imageCache] imageForUrl:request.URL.absoluteString + size:size + scale:scale + resizeMode:resizeMode + responseDate:@""]; + if (image) { + completionHandler(nil, image, @""); + return ^{ }; + } + + RCTNetworking *networking = [_bridge networking]; // Check if networking module can load image