From 978d5a2d4f4eede9cd6ba4786cae2dcc99ce3a37 Mon Sep 17 00:00:00 2001 From: Mihai Lazari Date: Wed, 4 Feb 2026 03:27:36 -0800 Subject: [PATCH] Fix expo-image-manipulator cropped image orientation (#55379) Summary: https://github.com/facebook/react-native/pull/54184 introduced a change that uses `CGImageSourceCreateImageAtIndex` instead of `CGImageSourceCreateThumbnailAtIndex` to decode full-sized images. However, unlike `CGImageSourceCreateThumbnailAtIndex` which rotates the image according to the orientation in the image's metadata, `CGImageSourceCreateThumbnailAtIndex` doesn't do that. Since the UIImage initializer is always passed `UIImageOrientationUp` for orientation, this results in the returned `UIImage` having wrong orientation. This can be reproduced for example if you take a photo with `react-native-vision-camera` in an orientation different from "up" (e.g. taking a photo on an iPhone SE 2020 in a vertical position results in a photo with "landscape-right" orientation), then use PhotoManipulator.crop() to crop the image, then show the resulted image URI in an Image component - the image is shown in a wrong orientation. This change fixes that by making sure the UIImage is passed the correct image orientation when `CGImageSourceCreateImageAtIndex` is used. ## Changelog: [IOS] [FIXED] - Fix expo-image-manipulator cropped image orientation bypass-github-export-checks Pull Request resolved: https://github.com/facebook/react-native/pull/55379 Test Plan: ### 0.82.1 https://github.com/mlazari/RNImageRepro/tree/rn-0.82.1 https://github.com/user-attachments/assets/297f6419-980b-49b5-873c-46c43bcc0fd6 ### 0.83.1 https://github.com/mlazari/RNImageRepro/tree/0.83.1 https://github.com/user-attachments/assets/286144e7-2196-49cb-b151-26f1a6ff274d ### 0.83.1 + patch with the changes in this PR https://github.com/mlazari/RNImageRepro/tree/0.83.1-with-patch https://github.com/user-attachments/assets/72ed6e74-a2cd-4b0d-b529-d91d0fdb3d52 Reviewed By: javache Differential Revision: D92148021 Pulled By: cipolleschi fbshipit-source-id: 0e485379b533fb415cb9d7946276a3de41657892 --- .../Libraries/Image/RCTImageUtils.mm | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/Image/RCTImageUtils.mm b/packages/react-native/Libraries/Image/RCTImageUtils.mm index d41a26f2112..c5b674850bf 100644 --- a/packages/react-native/Libraries/Image/RCTImageUtils.mm +++ b/packages/react-native/Libraries/Image/RCTImageUtils.mm @@ -55,6 +55,30 @@ static CGImagePropertyOrientation CGImagePropertyOrientationFromUIImageOrientati } } +static UIImageOrientation UIImageOrientationFromCGImagePropertyOrientation(CGImagePropertyOrientation imageOrientation) +{ + switch (imageOrientation) { + case kCGImagePropertyOrientationUp: + return UIImageOrientationUp; + case kCGImagePropertyOrientationDown: + return UIImageOrientationDown; + case kCGImagePropertyOrientationLeft: + return UIImageOrientationLeft; + case kCGImagePropertyOrientationRight: + return UIImageOrientationRight; + case kCGImagePropertyOrientationUpMirrored: + return UIImageOrientationUpMirrored; + case kCGImagePropertyOrientationDownMirrored: + return UIImageOrientationDownMirrored; + case kCGImagePropertyOrientationLeftMirrored: + return UIImageOrientationLeftMirrored; + case kCGImagePropertyOrientationRightMirrored: + return UIImageOrientationRightMirrored; + default: + return UIImageOrientationUp; + } +} + CGRect RCTTargetRect(CGSize sourceSize, CGSize destSize, CGFloat destScale, RCTResizeMode resizeMode) { if (CGSizeEqualToSize(destSize, CGSizeZero)) { @@ -269,6 +293,7 @@ UIImage *__nullable RCTDecodeImageWithData(NSData *data, CGSize destSize, CGFloa } NSNumber *width = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth); NSNumber *height = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight); + NSNumber *orientationNum = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation); CGSize sourceSize = {width.doubleValue, height.doubleValue}; CFRelease(imageProperties); @@ -293,12 +318,15 @@ UIImage *__nullable RCTDecodeImageWithData(NSData *data, CGSize destSize, CGFloa CGImageRef imageRef; BOOL createThumbnail = targetPixelSize.width != 0 && targetPixelSize.height != 0 && (sourceSize.width > targetPixelSize.width || sourceSize.height > targetPixelSize.height); + UIImageOrientation orientation = UIImageOrientationUp; if (createThumbnail) { CGFloat maxPixelSize = fmax(targetPixelSize.width, targetPixelSize.height); // Get a thumbnail of the source image. This is usually slower than creating a full-sized image, // but takes up less memory once it's done. + // It rotates the image according to the orientation from metadata, so we'll pass `UIImageOrientationUp` + // to the `UIImage` initializer imageRef = CGImageSourceCreateThumbnailAtIndex( sourceRef, 0, (__bridge CFDictionaryRef) @{ (id)kCGImageSourceShouldAllowFloat : @YES, @@ -313,6 +341,14 @@ UIImage *__nullable RCTDecodeImageWithData(NSData *data, CGSize destSize, CGFloa sourceRef, 0, (__bridge CFDictionaryRef) @{ (id)kCGImageSourceShouldAllowFloat : @YES, }); + + // Unlike `CGImageSourceCreateThumbnailAtIndex` (with `kCGImageSourceCreateThumbnailWithTransform` set to YES), + // `CGImageSourceCreateImageAtIndex` doesn't rotate the image to keep the orientation, so we'll need to pass + // the actual orientation (if present) to the `UIImage` initializer + if (orientationNum) { + orientation = UIImageOrientationFromCGImagePropertyOrientation( + (CGImagePropertyOrientation)[orientationNum unsignedIntValue]); + } } CFRelease(sourceRef); @@ -321,7 +357,7 @@ UIImage *__nullable RCTDecodeImageWithData(NSData *data, CGSize destSize, CGFloa } // Return image - UIImage *image = [UIImage imageWithCGImage:imageRef scale:destScale orientation:UIImageOrientationUp]; + UIImage *image = [UIImage imageWithCGImage:imageRef scale:destScale orientation:orientation]; CGImageRelease(imageRef); return image; }