【发布时间】:2019-02-26 00:17:32
【问题描述】:
我已经定义了一个 React Native 类,该类旨在用一个图像引用填充“tableView”单元格,该图像引用是该类的道具输入。这是使用 FlatList 调用的。但是,当使用图像源标记指定时,此类无法在包中找到输入图像引用。只有当它是硬编码引用时才能找到它。
类定义如下:
class ListItem extends React.PureComponent {
_onPress = () => {
this.props.onPressItem(this.props.index);
}
render() {
const item = this.props.item;
console.log("ChannelSelection Mix Button artwork: ", item.artwork);
return (
<TouchableHighlight
onPress={this._onPress}
underlayColor="transparent">
<View>
<View style={styles.rowContainer}>
<Image source={{ uri: item.artwork }} style={styles.buttonContainer} />
<View style={styles.mixButtonTitleContainer}>
<Text style={styles.mixButtonTitle}
numberOfLines={2}>{item.channelTitle}</Text>
</View>
</View>
</View>
</TouchableHighlight>
);
}
}
当调用这个类时,我收到一条警告:
Warning
Could not find image file:///Users/myname/Library/Developer/CoreSimulator/Devices/34AE8A68-E69D-4804-B3C4-96DE7A051B9B/data/Containers/Bundle/Application/C79368E9-3D1B-492C-B72B-9BF5C4D3262B/myApp.app/./Resources/MissingAlbumArt.png
打印 item.artwork 的控制台输出是:
'ChannelSelection Mix Button artwork: ', './Resources/MissingAlbumArt.png'
这样你就可以看到文件名被正确传入了。
请注意,如果我换行:
<Image source={{ uri: item.artwork }} style={styles.buttonContainer} />
到:
<Image source={require('./Resources/MissingAlbumArt.png')} style={styles.buttonContainer} />
然后错误消失。
那么,如何正确引用资源目录中的 .png 文件?为什么我的 .png 文件不在包的 Resources 目录中?
【问题讨论】:
标签: image react-native