react-native: [Android] Image.getSize returns wrong dimensions
- Review the documentation: https://facebook.github.io/react-native
- Search for existing issues: https://github.com/facebook/react-native/issues
- Use the latest React Native release: https://github.com/facebook/react-native/releases
Environment
React Native Environment Info: System: OS: macOS 10.14 CPU: x64 Intel® Core™ i5-5257U CPU @ 2.70GHz Memory: 47.52 MB / 8.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 8.11.1 - /usr/local/bin/node Yarn: 1.10.1 - /usr/local/bin/yarn npm: 6.4.1 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1 IDEs: Android Studio: 3.1 AI-173.4907809 Xcode: 10.1/10B61 - /usr/bin/xcodebuild npmPackages: react: 16.5.0 => 16.5.0 react-native: 0.57.1 => 0.57.1 npmGlobalPackages: react-native-cli: 2.0.1 react-native-create-library: 3.1.2 react-native-git-upgrade: 0.2.7
Description
Image.getSize does not return the correct width and height of the image. See the example below:
// uses RNCamera from 'react-native-camera'
takePicture = async (camera: any) => {
const options = {
quality: 0.5,
base64: false,
}
const metadata = await camera.takePictureAsync(options)
const { uri, width, height } = metadata
console.log({ width, height })
if (isAndroid) {
Image.getSize(uri, (w, h) => {
console.log({ width: w, height: h })
})
}
}
console.log results:
metadata from takePictureAsync: { width: 2592, height: 1944}
result from Image.getSize: { width: 1296, height: 972 }
I confirmed after exporting the picture that the real dimensions are the ones from takePictureAsync.
Reproducible Demo
see the example
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 15
So I had to do it. react-native-image-size:
Yes, it only happens with huge images. I could not dig deeper to confirm if that was it, but most likely it is.
In my use case, I need to crop the image using
ImageEditor.cropImage
. This method expects asize: { width: number, height: number }
parameter which defines the number of px to crop. These values are based on the real dimensions and not the ones given byImage.getSize
in these cases.Right know we are using the picture dimensions given by
takePictureAsync metadata
, although there are a few bugs related with delays while capturing the pictures in Android. Using the Image.getSize was a workaround to avoid blocking the screen for 2 to 3 seconds after capturing a picture.It is a big problem. Please add a new method like the old
Image.getSize
.