Skip to content

Commit 45896bc

Browse files
committed
Use CGImageSourceCreateImageAtIndex to decode full-sized images
1 parent e926ad0 commit 45896bc

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,28 @@ 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 createFullImage = targetPixelSize.width == 0 || targetPixelSize.height == 0 || (sourceSize.width <= targetPixelSize.width && sourceSize.height <= targetPixelSize.height);
298+
299+
if (createFullImage) {
300+
// Get an image in full size. This is faster than `CGImageSourceCreateThumbnailAtIndex`
301+
// and consumes less memory if only the target size doesn't require downscaling.
302+
imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)@{
303+
(id)kCGImageSourceShouldAllowFloat : @YES,
304+
});
305+
} else {
306+
CGFloat maxPixelSize = fmax(targetPixelSize.width, targetPixelSize.height);
307+
308+
// Get a thumbnail of the source image. This is usually slower than creating a full-sized image,
309+
// but takes up less memory once it's done.
310+
imageRef = CGImageSourceCreateThumbnailAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)@{
311+
(id)kCGImageSourceShouldAllowFloat : @YES,
312+
(id)kCGImageSourceCreateThumbnailWithTransform : @YES,
313+
(id)kCGImageSourceCreateThumbnailFromImageAlways : @YES,
314+
(id)kCGImageSourceThumbnailMaxPixelSize : @(maxPixelSize),
315+
});
316+
}
317+
308318
CFRelease(sourceRef);
309319
if (!imageRef) {
310320
return nil;

0 commit comments

Comments
 (0)