Is it possible to using Image.getSize with static

2020-02-25 07:06发布

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?

3条回答
走好不送
2楼-- · 2020-02-25 07:32

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

查看更多
虎瘦雄心在
3楼-- · 2020-02-25 07:39

Through official document

import { Image } from 'react-native'

const url = require('./x.png')
const image = Image.resolveAssetSource(url)

Then use image.width and image.height

查看更多
等我变得足够好
4楼-- · 2020-02-25 07:46

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`
查看更多
登录 后发表回答