Just getting started with React-Native and I'm having some trouble requiring a static image.
刚刚开始使用React-Native,我遇到了一些需要静态图像的问题。
Here's the very-basic code I have so far:
这是我到目前为止的基本代码:
'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);
The main trouble area is:
主要的麻烦区域是:
<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>
When packaging my app and running it, I get a render error:
打包我的应用程序并运行它时,我收到渲染错误:
I understand that you need to add images in xcode as an image set in order for the require
call to find the image and have added an image set to that end:
我知道您需要在xcode中添加图像作为图像集,以便要求调用查找图像并为此添加了图像集:
...But to no avail. Anyone have any thoughts? I've read the docs and seem to be doing this in the prescribed way. Thanks!
......但无济于事。有人有什么想法?我已经阅读了文档,似乎是以规定的方式做到了这一点。谢谢!
7 个解决方案
#1
42
Since React Native release 0.14
the handling of images has been normalized for iOS and Android and is now different. I could not find any documentation except for this very clear commit note on Github: Document new asset system.
自从React Native版本0.14开始,iOS和Android的图像处理已经标准化,现在已经不同了。除了关于Github的非常明确的提交说明之外,我找不到任何文档:记录新的资产系统。
There's only a screenshot of the proposed documentation:
只有提议文档的屏幕截图:
Update: There is a link now for the real documentation: https://facebook.github.io/react-native/docs/images.html
更新:现在有一个真实文档的链接:https://facebook.github.io/react-native/docs/images.html
#2
31
I had similar problem, then renamed the image files in file system under the xcode project's Images.xcassets
directories with suffixes and capitalization to match the image I'm loading.
我有类似的问题,然后重命名文件系统中的图像文件在xcode项目的Images.xcassets目录下,后缀和大小写匹配我正在加载的图像。
For example, if source={require('image!Villagers')}
it required the image files named precisely as Villagers.png
, Villagers@2x.png
and Villagers@3x.png
. If the '@3x' part of filename wasn't there, the image would not get found.
例如,如果source = {require('image!Villagers')},则需要精确命名为Villagers.png,Villagers @ 2x.png和Villagers@3x.png的图像文件。如果文件名的'@ 3x'部分不存在,则无法找到图像。
#3
4
What version of React Native are you running? There was an issue with the packager, related to that, that have been solved.
你在运行什么版本的React Native?与之相关的打包机存在问题,已经解决了。
If that's the case, you can either update or run the dev server with:
如果是这种情况,您可以使用以下命令更新或运行开发服务器:
node_modules/react-native/packager/packager.sh --assetRoots path/to/Images.xcassets
https://github.com/facebook/react-native/issues/282
https://github.com/facebook/react-native/issues/282
#4
3
This question has already been answered, but if you use React Native on Android, you might have that same problem. For me, solution was using source={{ uri: "google", isStatic: true }}
instead of source={required('./bg.png')}
.
此问题已得到解答,但如果您在Android上使用React Native,则可能会遇到同样的问题。对我来说,解决方案是使用source = {{uri:“google”,isStatic:true}}而不是source = {required('./ bg.png')}。
Just don't forget to add your images in src/main/res/drawable
folder.
只是不要忘记在src / main / res / drawable文件夹中添加图像。
#5
2
NOTE: App build required for new resources Any time you add a new resource to Images.xcassets you will need to re->build your app through XCode before you can use it - a reload from within >the simulator is not enough. (from React Native docs https://facebook.github.io/react-native/docs/image.html#content)
注意:新资源所需的应用程序构建每次向Images.xcassets添加新资源时,您都需要在使用之前通过XCode重新构建应用程序 - 从模拟器内部重新加载是不够的。 (来自React Native docs https://facebook.github.io/react-native/docs/image.html#content)
Also make sure to restart the packager. That solved the issue for me.
还要确保重新启动打包器。这解决了我的问题。
#6
1
You can easy to refer the images by making the package.json file in your assets folder.
您可以通过在assets文件夹中创建package.json文件来轻松引用图像。
项目文件夹结构
的package.json
And you can call it by this code.
您可以通过此代码调用它。
<Image
source={require('assets/img/avatar.png')}
style={styles.itemAvatar}
resizeMode={'cover'}
/>
#7
0
For static images, you need to provide the path like:
对于静态图像,您需要提供如下路径:
<Image source={require('./images/back.png')}
style= {{width:25, height:25, marginLeft:12, padding:12}}/>
This worked for me, for the image stored in images folder of project directory:
对我来说,这适用于存储在项目目录的images文件夹中的图像:
#1
42
Since React Native release 0.14
the handling of images has been normalized for iOS and Android and is now different. I could not find any documentation except for this very clear commit note on Github: Document new asset system.
自从React Native版本0.14开始,iOS和Android的图像处理已经标准化,现在已经不同了。除了关于Github的非常明确的提交说明之外,我找不到任何文档:记录新的资产系统。
There's only a screenshot of the proposed documentation:
只有提议文档的屏幕截图:
Update: There is a link now for the real documentation: https://facebook.github.io/react-native/docs/images.html
更新:现在有一个真实文档的链接:https://facebook.github.io/react-native/docs/images.html
#2
31
I had similar problem, then renamed the image files in file system under the xcode project's Images.xcassets
directories with suffixes and capitalization to match the image I'm loading.
我有类似的问题,然后重命名文件系统中的图像文件在xcode项目的Images.xcassets目录下,后缀和大小写匹配我正在加载的图像。
For example, if source={require('image!Villagers')}
it required the image files named precisely as Villagers.png
, Villagers@2x.png
and Villagers@3x.png
. If the '@3x' part of filename wasn't there, the image would not get found.
例如,如果source = {require('image!Villagers')},则需要精确命名为Villagers.png,Villagers @ 2x.png和Villagers@3x.png的图像文件。如果文件名的'@ 3x'部分不存在,则无法找到图像。
#3
4
What version of React Native are you running? There was an issue with the packager, related to that, that have been solved.
你在运行什么版本的React Native?与之相关的打包机存在问题,已经解决了。
If that's the case, you can either update or run the dev server with:
如果是这种情况,您可以使用以下命令更新或运行开发服务器:
node_modules/react-native/packager/packager.sh --assetRoots path/to/Images.xcassets
https://github.com/facebook/react-native/issues/282
https://github.com/facebook/react-native/issues/282
#4
3
This question has already been answered, but if you use React Native on Android, you might have that same problem. For me, solution was using source={{ uri: "google", isStatic: true }}
instead of source={required('./bg.png')}
.
此问题已得到解答,但如果您在Android上使用React Native,则可能会遇到同样的问题。对我来说,解决方案是使用source = {{uri:“google”,isStatic:true}}而不是source = {required('./ bg.png')}。
Just don't forget to add your images in src/main/res/drawable
folder.
只是不要忘记在src / main / res / drawable文件夹中添加图像。
#5
2
NOTE: App build required for new resources Any time you add a new resource to Images.xcassets you will need to re->build your app through XCode before you can use it - a reload from within >the simulator is not enough. (from React Native docs https://facebook.github.io/react-native/docs/image.html#content)
注意:新资源所需的应用程序构建每次向Images.xcassets添加新资源时,您都需要在使用之前通过XCode重新构建应用程序 - 从模拟器内部重新加载是不够的。 (来自React Native docs https://facebook.github.io/react-native/docs/image.html#content)
Also make sure to restart the packager. That solved the issue for me.
还要确保重新启动打包器。这解决了我的问题。
#6
1
You can easy to refer the images by making the package.json file in your assets folder.
您可以通过在assets文件夹中创建package.json文件来轻松引用图像。
项目文件夹结构
的package.json
And you can call it by this code.
您可以通过此代码调用它。
<Image
source={require('assets/img/avatar.png')}
style={styles.itemAvatar}
resizeMode={'cover'}
/>
#7
0
For static images, you need to provide the path like:
对于静态图像,您需要提供如下路径:
<Image source={require('./images/back.png')}
style= {{width:25, height:25, marginLeft:12, padding:12}}/>
This worked for me, for the image stored in images folder of project directory:
对我来说,这适用于存储在项目目录的images文件夹中的图像: