简体   繁体   English

反应本机图像未出现

[英]React-native Image not appearing

I am using React-Native v0.60.4, running on android simulator, generates an image by using react-native-signature-capture , the image can be opened manually and show correctly. 我正在使用在Android模拟器上运行的React-Native v0.60.4,通过使用react-native-signature-capture生成图像,该图像可以手动打开并正确显示。 I want to display this image by using Image , but it displays nothing. 我想使用Image显示此图像,但是什么也不显示。

I checked but there is no error/warning message. 我检查了,但是没有错误/警告消息。

<Image
     style={{width: 200, height: 200}}
     source={{uri: '/storage/emulated/0/saved_signature/signature.png'}}
/>

This should be very basic usage of Image but why nothing displayed? 这应该是Image非常基本的用法,但是为什么什么都不显示?

Use the imagepicker to bring the image in the path, use the image module , and look at the image . 使用imagepicker带来的image的路径,使用image module ,并查看image

Example

import ImagePicker from 'react-native-image-picker';
...
  ImagePicker.showImagePicker(options, response => {
        let source = response;
        this.setState({
          filePath: source,
        });

...
<Image 
          source={{ uri: this.state.filePath.uri}} 
          style={{width: 100, height: 100}} />

if you want to show a local image than you have to use require property instead of uri like this:- 如果要显示本地图像,则必须使用require属性而不是uri,如下所示:-

image from local storage:- 来自本地存储的图像:-

<Image 
style={{width: 200, height: 200}}
source={require('/storage/emulated/0/saved_signature/signature.png')} />

For dynamic image or image from uri:- 对于动态图像或来自uri的图像:-

<Image 
style={{width: 200, height: 200}}
source={{uri: 'https://facebook.github.io/react/logo-og.png'}} />

To display an image, you need to require it before 要显示图像,您需要先要求它

import myImage from './path_to_image.png'

<Image source={myImage} style={{ width: 200, height: 200 }} />

For more information, you have this documentation 有关更多信息,请参阅此文档。

You need to pass in a require() to source prop like so: 你需要传递一个require()source道具,像这样:

const ImageDetail = props => {
  return (
    <View>
      <Image source={require('/storage/emulated/0/saved_signature/signature.png')} />
      <Text>{props.title}</Text>
    </View>
  );
};

Make sure your path is correct as well in that its not ../storage or ../../storage . 确保您的路径也是正确的,因为它不是../storage../../storage

And all this would be in your child component for example, keeping in mind that you did not specify which component you are on. 例如,所有这些都将存在于您的子组件中,请记住,您没有指定所使用的组件。 Once you have ensured the image renders then you want to go to your parent component and pass it the prop of whatever name you choose, for example imageSource . 确保图像已渲染后,您便要转到父组件并将其传递给您选择的任何名称的道具,例如imageSource

Then you pass in that require again and the relative path: 然后,您再次传递该要求和相对路径:

const ImageScreen = () => {
  return (
    <View>
      <ImageDetail
        title='Signature One'
        imageSource={require("/storage/emulated/0/saved_signature/signature1.png")}
      />
      <ImageDetail
        title='Signature Two'
        imageSource={require("/storage/emulated/0/saved_signature/signature2.png")}
      />
      <ImageDetail
        title='Signature Three'
        imageSource={require("/storage/emulated/0/saved_signature/signature3.png")}
      />
    </View>
  );
};

I would then console log props on that child component and if your setup is similar to whats above you should see: 然后,我将在该子组件上控制台日志道具,如果您的设置类似于上面的内容,则应该看到:

{title: "Signature One", imageSource: 3}
{title: "Signature Two", imageSource: 4}
{title: "Signature Three", imageSource: 5}

The 3, 4, 5 would be an identifier so don't worry about that, but I put it there just in case you see it. 3、4、5将是一个标识符,因此不必担心,但我将其放在此处,以防万一您看到它。

Now in your child component you can remove that require('/storage/emulated/0/saved_signature/signature.png') from your source={} prop and replace it with <Image source={props.imageSource} /> or whatever you called it. 现在,在子组件中,您可以从source={} prop中删除该require('/storage/emulated/0/saved_signature/signature.png') ,然后将其替换为<Image source={props.imageSource} />或任何其他内容你叫了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM