question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] Image.getSize returns wrong dimensions

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:15

github_iconTop GitHub Comments

4reactions
eXist-FraGGercommented, Feb 19, 2019

So I had to do it. react-native-image-size: NPM version

4reactions
Fsarmentocommented, Nov 27, 2018

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 a size: { 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 by Image.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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expo React-Native getSize() method returns wrong height and ...
I have project where I am trying to get remote image dimensions with getSize() method, problem is that this method ...
Read more >
react-native-image-crop-picker - npm
iOS/Android image picker with support for camera, video, configurable compression, multiple images and cropping ...
Read more >
getimagesize - Manual - PHP
The getimagesize() function will determine the size of any supported given image file and return the dimensions along with the file type and...
Read more >
React Native Get Remote Image Dimensions Android iOS
getSize () function of Image Component In React Native : Retrieve the width and height (in pixels) of an image prior to displaying...
Read more >
Image - React Native
repeat : Repeat the image to cover the frame of the view. The image will keep its size and aspect ratio, unless it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found