Skip to content

Commit 61b013e

Browse files
danilobuergerfacebook-github-bot
authored andcommitted
Allow modifying iOS image cache limits (#33554)
Summary: Allow modifying iOS image cache limits. Currently the image cache imposes some strict limits: [NSCache.totalCostLimit](https://developer.apple.com/documentation/foundation/nscache/1407672-totalcostlimit?language=objc) of 20MB. Single Image Size Limit of 2MB (bitmap size). This may not be enough for applications that make heavy use of images. With this commit it is possible to fine tune them to the applications need. This can be set for example in the App Delegate with: ```objc RCTSetImageCacheLimits(4*1024*1024, 200*1024*1024); ``` This would increase the single image size limit to 4 MB and the total cost limit to 200 MB. ## Changelog [iOS] [Added] - Allow modifying iOS image cache limits Pull Request resolved: #33554 Test Plan: There is no easy way to test this except adding the above snippet and checking via break points that the new limits are used. Reviewed By: cipolleschi Differential Revision: D35485914 Pulled By: cortinico fbshipit-source-id: 646cf7cab5ea5258d0d0d0bce6383317e27e4445
1 parent 09b0648 commit 61b013e

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Libraries/Image/RCTImageCache.h

+3
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@
3939
@end
4040

4141
@interface RCTImageCache : NSObject <RCTImageCache>
42+
43+
RCT_EXTERN void RCTSetImageCacheLimits(NSUInteger maxCachableDecodedImageSizeInBytes, NSUInteger imageCacheTotalCostLimit);
44+
4245
@end

Libraries/Image/RCTImageCache.m

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818

1919
#import <React/RCTImageUtils.h>
2020

21-
static const NSUInteger RCTMaxCachableDecodedImageSizeInBytes = 2097152; // 2 MB
21+
static NSUInteger RCTMaxCachableDecodedImageSizeInBytes = 2*1024*1024;
22+
static NSUInteger RCTImageCacheTotalCostLimit = 20*1024*1024;
23+
24+
void RCTSetImageCacheLimits(NSUInteger maxCachableDecodedImageSizeInBytes, NSUInteger imageCacheTotalCostLimit) {
25+
RCTMaxCachableDecodedImageSizeInBytes = maxCachableDecodedImageSizeInBytes;
26+
RCTImageCacheTotalCostLimit = imageCacheTotalCostLimit;
27+
}
2228

2329
static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat scale,
2430
RCTResizeMode resizeMode)
@@ -38,7 +44,7 @@ - (instancetype)init
3844
{
3945
if (self = [super init]) {
4046
_decodedImageCache = [NSCache new];
41-
_decodedImageCache.totalCostLimit = 20 * 1024 * 1024; // 20 MB
47+
_decodedImageCache.totalCostLimit = RCTImageCacheTotalCostLimit;
4248
_cacheStaleTimes = [NSMutableDictionary new];
4349

4450
[[NSNotificationCenter defaultCenter] addObserver:self

0 commit comments

Comments
 (0)