Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to using Image.getSize with static image file?

I want to use Image.getSize (https://facebook.github.io/react-native/docs/image.html) to get the size of my image but the first argument require the image source to be in URI but I can't use URI with static file, I can only use Require.

Therefore, is it possible is to use Image.getSize on static file or do I have to find another way?

like image 664
binkpitch Avatar asked Feb 02 '17 08:02

binkpitch


2 Answers

You can use resolveAssetSource (from react-native/Libraries/Image/resolveAssetSource) to get the image's size.

import resolveAssetSource from 'resolveAssetSource';

and

let icon =  require('./assets/images/icon.png'); 
let source = resolveAssetSource(icon);
// source.width, source.height`
like image 193
PTT Avatar answered Sep 19 '22 23:09

PTT


You can use Image.resolveAssetSource(source).width and Image.resolveAssetSource(source).height.

Reference in React Native docs: https://facebook.github.io/react-native/docs/image.html#resolveassetsource

like image 35
Benoît Vogel Avatar answered Sep 20 '22 23:09

Benoît Vogel