简体   繁体   English

无法在本机中加载图像

[英]Unable to load images in react-native

I got stuck with loading images in react-native. 我陷入了将图像加载到本机的状态。 I went through all the solutions given in the similar questions posted but the answers are not helpfull in my case. 我经历了类似问题中给出的所有解决方案,但答案对我而言没有帮助。 I tried importing the images like how we do in react like using import or require but both the options are not working. 我尝试导入图像,就像我们在使用import或require这样的反应中所做的一样,但是这两个选项均不起作用。

PFB code for your ref PFB代码供您参考

import React, { Component } from 'react';
import { Card, CardTitle, CardContent, CardAction, CardButton, CardImage } from 'react-native-material-cards';
import aboutImage from "https://images.pexels.com/photos/34950/pexels-photo.jpg";
import {
  Button,
  View,
  Text,
  ScrollView, StyleSheet
} from 'react-native'

class About extends Component {

  handleScroll = e => {

  }

  render() {
    return (
      <ScrollView onScroll={this.handleScroll}>
        <View style={styles.container}>
          <Text style={styles.text}>Who we are</Text>
          <Card style={{backgroundColor: "#fff", boxShadow: "2px 3px #3333"}}>
            <CardImage 
              source={{uri:aboutImage }}
              title="About US"
            />
            <CardContent text="Testing" />
          </Card>
      </View>
    </ScrollView>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
    margin: 20
  },
  text:{
    color: "#06357a",
    fontWeight: "bold",
    fontSize: 30,
    fontFamily: "FreeSans,Arimo,Droid Sans,Helvetica,Arial,sans-serif"
  },
  text1:{
    color: "#06357a",
    fontWeight: "bold",
    fontFamily: "FreeSans,Arimo,Droid Sans,Helvetica,Arial,sans-serif"
  }
});

export default About;

.babelrc .babelrc

{
  "presets": ["module:metro-react-native-babel-preset"]
}

Edit 编辑

I tried downloading images to local and use require to import them but I get below error 我尝试将图像下载到本地并使用require导入它们,但出现以下错误

error: bundling failed: Error: Unable to resolve module ../../../../assets/AboutUS.jpg from D:\\react-native\\AppUsingRN\\components\\Layout\\components\\About.js : The module ../../../../assets/AboutUS.jpg could not be found from D:\\react-native\\AppUsingRN\\components\\Layout\\components\\About.js . 错误:捆绑失败:错误:无法从D:\\react-native\\AppUsingRN\\components\\Layout\\components\\About.js解析模块../../../../assets/AboutUS.jpg :模块../../../../assets/AboutUS.jpg无法从找到D:\\react-native\\AppUsingRN\\components\\Layout\\components\\About.js Indeed, none of these files exist: 实际上,这些文件都不存在:

I imported it like 我像导入

   <CardImage 
          source={{uri: require('../../../../assets/AboutUS.jpg')}} 
          title="About US"
        />

I am not able to figure out the issue. 我无法解决问题。

It should work with: 它应该与:

<CardImage 
  source={{ uri: "https://images.pexels.com/photos/34950/pexels-photo.jpg" }}
  title="About US"
/>

if use require 如果require使用

 const imageByRequire = require('../../../../assets/AboutUS.jpg'); <CardImage source={imageByRequire} title={'About US'} /> 

if use uri directly 如果直接使用uri

 const imageByUri = "https://images.pexels.com/photos/34950/pexels-photo.jpg"; <CardImage source={imageByUri} title={'About US'} /> 

If "It's not working somehow", maybe downloading or download error(there would be nothing), perhaps you can plus defaultSource . 如果“以某种方式无法正常工作”,可能是下载或下载错误(什么都没有),也许您可​​以加上defaultSource

 const imageByUri = "https://images.pexels.com/photos/34950/pexels-photo.jpg"; const defaultImage = require('../../../../assets/AboutUS.jpg'); <CardImage defaultSource={defaultImage} source={imageByUri} title={'About US'} /> 

hope helpful :) 希望对您有所帮助:)

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

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