我正在尝试显示我的计算机文件中的图像。然而,它不会出现。我做了一些调试,发现当我将我的目录设置为这个时:
source={{uri:'http://www.telegraph.co.uk/content/dam/Travel/galleries/travel/activityandadventure/The-worlds-most-beautiful-mountains/mountains-fitzroy_3374108a.jpg'}}
它按照我想要的方式在屏幕中央显示图像。我不想使用网络地址,我想使用相对地址。所以我用这个:
source={{uri:'/./pictures/m.jpg'}}
当我执行此操作时,它没有图像或出现错误。 如何让图像出现在相对目录中? 这是我的全部代码:
import React, { Component } from 'react';
import { Button,Alert, TouchableOpacity,Image, Dimensions } from 'react-native'
import {
AppRegistry,
StyleSheet,
Text,
View,
} from 'react-native';
class Project extends Component {
render() {
return (
<View style={{backgroundColor: '#375D81', flex: 1}}>
<View style = {styles.container}>
<Image source={{uri: './pictures/Blur.png/'}} style={{ resizeMode: 'cover', width: 100, height: 100 }}/>
<TouchableOpacity style = {styles.buttonText1} onPress={() => { Alert.alert('You tapped the button!')}}>
<Text style={styles.text}>
Button 1
</Text>
</TouchableOpacity>
<TouchableOpacity style = {styles.buttonText2} onPress={() => { Alert.alert('You tapped the button!')}}>
<Text style= {styles.text}>
Button 2
</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
最佳答案
当使用本地目录时,最好使用require
函数,例如:
<Image source={{require('./pictures/Blur.png/')}} style={{ resizeMode: 'cover', width: 100, height: 100 }}/>
关于android - 找不到图像的相对/本地目录 - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47230340/