【发布时间】:2015-06-01 06:26:27
【问题描述】:
刚开始使用React-Native,我在需要静态图片时遇到了一些问题。
这是我目前拥有的非常基本的代码:
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
Image,
View,
} = React;
var buzz = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Image source={require('image!bg')} style={styles.bg}>
<Text style={styles.welcome}>
Welcome to Buzz! iOS interface to
</Text>
<Text style={styles.welcomeHeader}>Charityware</Text>
</Image>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'transparent'
},
welcomeHeader: {
fontWeight: '600',
fontFamily: 'Helvetica',
color: '#fff',
fontSize: 50,
alignSelf: 'center'
},
bg: {
width: 400,
height: 300,
alignSelf: 'auto',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
},
});
AppRegistry.registerComponent('buzz', () => buzz);
主要的问题是:
<Image source={require('image!bg')} style={styles.bg}>
<Text style={styles.welcome}>
Welcome to Buzz! iOS interface to
</Text>
<Text style={styles.welcomeHeader}>Charityware</Text>
</Image>
打包我的应用程序并运行它时,出现渲染错误:
我了解您需要在 xcode 中将图像添加为图像集,以便 require 调用找到图像并为此添加图像集:
...但无济于事。有人有什么想法吗?我已阅读文档并且 似乎 正在以规定的方式执行此操作。谢谢!
【问题讨论】:
-
在我们的项目中,我们在 Image 中使用 require('../../img/some.png') 以便像 Android & iOS 这样的跨平台团队可以在代码中使用相同的资源。
标签: javascript ios xcode reactjs react-native