Skip to content

Commit

Permalink
[Airbnb] Fix Image Cache issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandrichardson authored and Gabriel Peal committed Jul 23, 2017
1 parent 6e60d2c commit 647b0c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Libraries/Image/RCTImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions Libraries/Image/RCTImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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
{
Expand All @@ -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
Expand Down

0 comments on commit 647b0c9

Please sign in to comment.