react-native-fast-image icon indicating copy to clipboard operation
react-native-fast-image copied to clipboard

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

Open rocwind opened this issue 5 years ago • 22 comments

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

rocwind avatar Apr 30 '19 04:04 rocwind

same issue!

ryanypm avatar Apr 30 '19 04:04 ryanypm

same issue

liuhy412484577 avatar Apr 30 '19 08:04 liuhy412484577

same issue

saykalik avatar May 17 '19 19:05 saykalik

same issue! (It gives the view size, not the natural size).

yungkittty avatar May 19 '19 05:05 yungkittty

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 ))

tjbenton avatar Jun 13 '19 20:06 tjbenton

same issue

Dylan0916 avatar Aug 08 '19 04:08 Dylan0916

Yeah, I've got the same problem, but only on Android 7.1 (and maybe lower, but I didn't test it) Android 8+ image sizes works as expected

bohdan145 avatar Sep 07 '19 22:09 bohdan145

Same issue !!! Android 9. RN 0.60.4

ducbau275 avatar Oct 01 '19 02:10 ducbau275

Same issue on Android 8.0 RN 0.59

pzxbc avatar Oct 29 '19 07:10 pzxbc

I found that in4core have solved this problem. please refer to this commit https://github.com/DylanVann/react-native-fast-image/commit/3e8f2e4104bdca5eea63aeee7499018f3c3da5fc

Also, you have to comment REACT_ON_LOAD_EVENT in FastImageRequestListener.onResourceReady, otherwise you will receive this event twice

It works on Android 8.0 & RN 0.59

pzxbc avatar Oct 29 '19 09:10 pzxbc

This is still an issue. ¿Any updates? ¿Any workarounds?

lschuft avatar Feb 22 '20 06:02 lschuft

I’ve used Image.getSize for Android

Отправлено с iPhone

22 февр. 2020 г., в 08:46, Lucas Schuft [email protected] написал(а):

 This is still an issue. ¿Any updates? ¿Any workarounds?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

bohdan145 avatar Feb 22 '20 07:02 bohdan145

The nativeEvent property on the onload event is null on android.

image

image

ahartzog avatar Feb 24 '20 14:02 ahartzog

same issue on IOS

mark5566 avatar Mar 21 '20 02:03 mark5566

Looks like we need adopt this code to get real image size: https://github.com/bumptech/glide/issues/781#issuecomment-160953996

Strate avatar May 19 '20 17:05 Strate

same issue on Android 9.0 RN 0.62.2

STIENLILLY avatar Jun 01 '20 14:06 STIENLILLY

same issue on android. ios working fine. RN 0.62.2

fattahmuhyiddeen avatar Jun 17 '20 10:06 fattahmuhyiddeen

Please try using this resizeMode={FastImage.resizeMode.stretch}

chenqi777 avatar Sep 08 '20 10:09 chenqi777

Still happening, even when using 'stretch' as suggested. RN 0.63.3 & RNFastImage 8.3.2

nadav2051 avatar Nov 05 '20 10:11 nadav2051

Any news? Same issue 0.65.1

KonstantinZhukovskij avatar Oct 01 '21 11:10 KonstantinZhukovskij


    const height = Platform.OS === 'android' ? ((e.nativeEvent?.source?.height * baseWidth) / e.nativeEvent?.source?.width) : (e.nativeEvent.height * baseWidth) / e.nativeEvent.width
    const imgStyle = {
      width: baseWidth,
      height: height || baseWidth
    };
    this.setState({ imgStyle: imgStyle })

rn 0.67.3 can be used.

suwu150 avatar Apr 12 '22 08:04 suwu150

e.nativeEvent?.source = null @suwu150

fukemy avatar Aug 30 '22 11:08 fukemy