react-native: Image.getSize not returning correct image dimension from url
Description

- Image is taken from ‘react-native’;
The above getSize method from react-native returns the minimum of dp of the screen and size of image(height/width) For example, dp = 1920 image width = 800 The returned width is 800
If image width. = 3000 The returned width is 1920.
Shouldn’t the getSize return the size of the image?
Version
0.66.4
Output of npx react-native info
System: OS: macOS 12.3 CPU: (8) x64 Apple M1 Pro Memory: 25.67 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 14.17.5 - /usr/local/bin/node Yarn: 1.22.17 - /usr/local/bin/yarn npm: 6.14.14 - /usr/local/bin/npm Watchman: 2022.03.21.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.2 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.4, iOS 15.4, macOS 12.3, tvOS 15.4, watchOS 8.5 Android SDK: API Levels: 29, 30, 31 Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.2, 30.0.3, 31.0.0, 32.0.0 System Images: android-25 | Google APIs ARM EABI v7a, android-28 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom_64, android-Sv2 | Google APIs ARM 64 v8a Android NDK: Not Found IDEs: Android Studio: 2021.1 AI-211.7628.21.2111.8139111 Xcode: 13.3/13E113 - /usr/bin/xcodebuild Languages: Java: 11.0.11 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 17.0.2 => 17.0.2 react-native: 0.66.4 => 0.66.4 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found
Steps to reproduce
- Use a 1920x1080 resolution screen.
- Grab a 4k or 8k image.
- Use the getSize method of Image to get the size of the image.
Snack, code example, screenshot, or link to a repository
No response
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 19 (2 by maintainers)
It’s absolutely ridiculous that you can’t even get the size of an image. Still. After over 6-7 years.
From what I understood in my investigation, it’s because they are using Fresco under the hood on Android and when Fresco measures an image it will take pixel density into account, I dont know if newer versions of Fresco have a method to avoid using pixel density and get the real size, or if there is an alternative to Fresco (although since Fresco is a Facebook lib I doubt they would switch), but this is really annoying, especially because it is not documented and clearly never worked properly since the Fresco update in 2018.
How is this issue closed ? the base functionnality just does not work, I just had to debug for an hour to find out that
Image.getSizebasically never worked in react nativewas anyone able to find a work around?
Looking forward to knowing more…
As an other alternative,
getImageMetaDatafromreact-native-compressoralso gives the correct dimensions, would love to see a fix in react native though and not have to rely on 3rd parties to do this.I ended up using react-native-image-resizer library to compress and get the real height and width for the new image. Setting high target resolution shouldn’t compress the image so you can retain the same resolution.
Hi @Marcoo09,
Thank you for the reply. The package that you have mentioned does work but if you look into the library, they convert the image to a bitmap and then get the real image dimension from it. It works okay for small images but for large images, it takes too much processing time directly hampering the app flow. I have addressed this in the git repo as well. https://github.com/eXist-FraGGer/react-native-image-size/issues/13
Also thank you for reopening the issue on the fresco.