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.

Image size get from onLoad() event is incorrect on Android

See original GitHub issue

The width and height get from onLoad() event is incorrect on Android. It shows 720x1192 (depends on the screen size) for below example code on Android, and it works fine on iOS (400x400).

    "react-native": "^0.59.3",
    "react-native-fast-image": "^5.3.0"
onLoad = (evt) => {
    console.warn(evt.nativeEvent);
}
...
        <FastImage
          onLoad={this.onLoad}
          source={{
            uri: 'https://unsplash.it/400/400?image=1'
          }}
          style={styles.image}
        />
...

full example project can be found at: https://github.com/rocwind/fast-image-test

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:22

github_iconTop GitHub Comments

4reactions
fattahmuhyiddeencommented, Jun 17, 2020

same issue on android. ios working fine. RN 0.62.2

4reactions
tjbentoncommented, Jun 13, 2019

the onLoad function is just passed down to the react-native Image component so the issue is more likely with react native it’s self. That being said there’s a workaround for it.

For server images you can use Image.getSize, and for local images you can use Image.resolveAssetSource. I’m not sure why getSize doesn’t just support both but it doesn’t so I would just write a util function like this.

function getSize (_) {
  // resolve the source and use it instead
  const src = ReactImage.resolveAssetSource(_)

  return new Promise((resolve, reject) => {
    if (!src) {
      reject(new Error('must pass in a valid source'))
    } else if (src.uri) {
      ReactImage.getSize(
        src.uri,
        (width, height) => resolve({ width, height }),
        reject
      )
    } else {
      resolve({ width: src.width, height: src.height })
    }
  })
}

then in your code you can just run it and use the result to set the state on your component

getSize(this.props.source)
  .then(({ width, height }) => this.setState({ width, height ))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting image width on image load fails on IE - Stack Overflow
On every image load a call this function with image and resize if its width or height is bigger than my max width...
Read more >
Slow rendering - Android Developers
Ensure that the bitmap being displayed isn't significantly bigger than the area on screen it's showing in. If it is, that wastes upload...
Read more >
Window: load event - Web APIs - MDN Web Docs - Mozilla
The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts, iframes, and images.
Read more >
Issue 67413 in chromium: get image size in img onload function
My case is related to this issue I think: Essentially javascript detect a wrong width of an image loaded from cache. ... 2)...
Read more >
'onload' event is not fired if HTTP Content-Length is invalid.
At first, I completely agree that it is a sever's wrong behavior. Instead of that, Chromium is trying hard to render the contents,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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