Skip to content

Commit be4fcda

Browse files
tsapetameta-codesync[bot]
authored andcommitted
Improve decoding full-sized images in RCTImageLoader (#54184)
Summary: Pull Request resolved: #54184 In Expo, someone reported (expo/expo#40158) that `expo-image-manipulator` causes memory crashes when processing large images (> 30MB). Image manipulator uses `RCTImageLoader` to load and decode images. As opposed to the Image component, it always requests for images in full size. For large images it may crash at `CGImageSourceCreateThumbnailAtIndex` which in the provided repro slowly increases memory usage until it finally crashes after a few seconds of running. I figured out that not including the `kCGImageSourceThumbnailMaxPixelSize` option works much better, but using `CGImageSourceCreateImageAtIndex` instead of `CGImageSourceCreateThumbnailAtIndex` works even better – it's faster and consumes less memory during decoding. This is more or less what `SDWebImage` library does, see [`SDImageIOAnimatedCoder`](https://github.com/SDWebImage/SDWebImage/blob/master/SDWebImage/Core/SDImageIOAnimatedCoder.m#L488-L509). With the proposed changes, it's still crashing but only for the largest image (64MB), other images (40MB and 50MB) are now working fine. Obviously, it cannot be fixed entirely and it's not recommended to load such big images without downscaling them. ## Changelog: [IOS] [CHANGED] - Use `CGImageSourceCreateImageAtIndex` instead of `CGImageSourceCreateThumbnailAtIndex` to decode full-sized images Pull Request resolved: #54127 Test Plan: I've tested the examples of the `Image` component in RNTester as well as the repro provided in expo/expo#40158 Reviewed By: javache Differential Revision: D84835416 Pulled By: cipolleschi fbshipit-source-id: a182dd00f00194f0463ad4f583cc695647414fca
1 parent 269b0bd commit be4fcda

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

packages/react-native/Libraries/Image/RCTImageUtils.mm

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,31 @@ BOOL RCTUpscalingRequired(
293293
// Calculate target size
294294
CGSize targetSize = RCTTargetSize(sourceSize, 1, destSize, destScale, resizeMode, NO);
295295
CGSize targetPixelSize = RCTSizeInPixels(targetSize, destScale);
296-
CGFloat maxPixelSize =
297-
fmax(fmin(sourceSize.width, targetPixelSize.width), fmin(sourceSize.height, targetPixelSize.height));
298-
299-
NSDictionary<NSString *, NSNumber *> *options = @{
300-
(id)kCGImageSourceShouldAllowFloat : @YES,
301-
(id)kCGImageSourceCreateThumbnailWithTransform : @YES,
302-
(id)kCGImageSourceCreateThumbnailFromImageAlways : @YES,
303-
(id)kCGImageSourceThumbnailMaxPixelSize : @(maxPixelSize),
304-
};
305-
306-
// Get thumbnail
307-
CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)options);
296+
CGImageRef imageRef;
297+
BOOL createThumbnail = targetPixelSize.width != 0 && targetPixelSize.height != 0 &&
298+
(sourceSize.width > targetPixelSize.width || sourceSize.height > targetPixelSize.height);
299+
300+
if (createThumbnail) {
301+
CGFloat maxPixelSize = fmax(targetPixelSize.width, targetPixelSize.height);
302+
303+
// Get a thumbnail of the source image. This is usually slower than creating a full-sized image,
304+
// but takes up less memory once it's done.
305+
imageRef = CGImageSourceCreateThumbnailAtIndex(
306+
sourceRef, 0, (__bridge CFDictionaryRef) @{
307+
(id)kCGImageSourceShouldAllowFloat : @YES,
308+
(id)kCGImageSourceCreateThumbnailWithTransform : @YES,
309+
(id)kCGImageSourceCreateThumbnailFromImageAlways : @YES,
310+
(id)kCGImageSourceThumbnailMaxPixelSize : @(maxPixelSize),
311+
});
312+
} else {
313+
// Get an image in full size. This is faster than `CGImageSourceCreateThumbnailAtIndex`
314+
// and consumes less memory if only the target size doesn't require downscaling.
315+
imageRef = CGImageSourceCreateImageAtIndex(
316+
sourceRef, 0, (__bridge CFDictionaryRef) @{
317+
(id)kCGImageSourceShouldAllowFloat : @YES,
318+
});
319+
}
320+
308321
CFRelease(sourceRef);
309322
if (!imageRef) {
310323
return nil;

0 commit comments

Comments
 (0)