Repository navigation
fix crop image regression #83795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix crop image regression #83795
Changes from all commits
f11bc33
9350047
3181ef8
0139eb0
9628cfd
060bdb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import type {LayoutChangeEvent} from 'react-native'; | |
| import {View} from 'react-native'; | ||
| import {Gesture, GestureDetector, GestureHandlerRootView} from 'react-native-gesture-handler'; | ||
| import type {GestureUpdateEvent, PanGestureChangeEventPayload, PanGestureHandlerEventPayload} from 'react-native-gesture-handler'; | ||
| import ImageSize from 'react-native-image-size'; | ||
| import Animated, {useAnimatedStyle, useSharedValue} from 'react-native-reanimated'; | ||
| import Image from '@components/Image'; | ||
| import RESIZE_MODES from '@components/Image/resizeModes'; | ||
|
|
@@ -58,6 +59,7 @@ function ReceiptCropView({imageUri, onCropChange, initialCrop, isAuthTokenRequir | |
| const cropY = useSharedValue(0); | ||
| const cropWidth = useSharedValue(0); | ||
| const cropHeight = useSharedValue(0); | ||
| const isImageInitialized = useRef(false); | ||
|
|
||
| // Track previous values to detect changes and recalculate crop on resize | ||
| const prevDisplayValuesRef = useRef<{ | ||
|
|
@@ -71,6 +73,25 @@ function ReceiptCropView({imageUri, onCropChange, initialCrop, isAuthTokenRequir | |
|
|
||
| const isCropInitialized = imageSize.width > 0 && imageSize.height > 0 && containerSize.width > 0 && containerSize.height > 0; | ||
|
|
||
| // Use react-native-image-size to get actual original image dimensions (not downsampled display dimensions). | ||
| // On Android, expo-image's onLoad reports Glide-downsampled dimensions, which would make crop coordinates | ||
| // wrong for expo-image-manipulator (which operates on full-resolution images). See AvatarCropModal for the same pattern. | ||
| useEffect(() => { | ||
| if (!imageUri || isAuthTokenRequired) { | ||
| return; | ||
| } | ||
| ImageSize.getSize(imageUri).then(({width, height, rotation: originalRotation}) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we handle the image processing error?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just updated and we also handle in the onLoad event and set the image size when it's not initialized so we don't need an error here. |
||
| // On Android, ImageSize returns rotation; when image is rotated 90/270°, swap width/height for layout. | ||
| if (originalRotation === 90 || originalRotation === 270) { | ||
| setImageSize({width: height, height: width}); | ||
| } else { | ||
| setImageSize({width, height}); | ||
| } | ||
|
|
||
| isImageInitialized.current = true; | ||
| }); | ||
| }, [imageUri, isAuthTokenRequired]); | ||
|
|
||
| // Calculate scale factors to convert display coordinates to image coordinates | ||
| const {scaleX, scaleY, displayWidth, displayHeight, imageOffsetX, imageOffsetY} = useMemo(() => { | ||
| if (!containerSize.width || !containerSize.height || !imageSize.width || !imageSize.height) { | ||
|
|
@@ -244,7 +265,9 @@ function ReceiptCropView({imageUri, onCropChange, initialCrop, isAuthTokenRequir | |
| if (!hasImageDimensions) { | ||
| setHasImageDimensions(true); | ||
| } | ||
| setImageSize({width, height}); | ||
| if (!isImageInitialized.current) { | ||
| setImageSize({width, height}); | ||
| } | ||
| }, | ||
| [hasImageDimensions, imageSize.width, imageSize.height], | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.