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

帅比萌擦擦* 提交于 2019-11-29 06:20:29

问题


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?


回答1:


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`



回答2:


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:


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



来源:https://stackoverflow.com/questions/41997611/is-it-possible-to-using-image-getsize-with-static-image-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!