在React Native中需要图像模块的麻烦

mar*_*mas 42 javascript xcode ios reactjs react-native

刚刚开始使用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);
Run Code Online (Sandbox Code Playgroud)

主要的麻烦区域是:

    <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>
Run Code Online (Sandbox Code Playgroud)

打包我的应用程序并运行它时,我收到渲染错误: 错误图片

我知道您需要在xcode中添加图像作为图像集,以便require调用查找图像并为此添加图像集: bg图像

......但无济于事.有人有什么想法?我已经阅读了文档,似乎是以规定的方式做到这一点.谢谢!

jla*_*tre 45

自React Native发布0.14以来,iOS和Android的图像处理已经标准化,现在已经不同了.除了关于Github的非常明确的提交说明之外,我找不到任何文档:记录新的资产系统.

只有提议文档的屏幕截图: 在此输入图像描述

更新:现在有一个真实文档的链接:https: //facebook.github.io/react-native/docs/images.html

  • 没有办法使用变量来获取本地图像吗? (2认同)

McG*_*McG 31

我有类似的问题,然后重命名文件系统中的图像文件在xcode项目的Images.xcassets目录下,后缀和大小写匹配我正在加载的图像.

例如,如果 source={require('image!Villagers')}它需要精确命名为的图像文件Villagers.png,Villagers@2x.pngVillagers@3x.png.如果文件名的'@ 3x'部分不存在,则无法找到图像.

  • 这对我有用.此外,不要忘记给图像一个宽度和高度,否则它将不会显示. (3认同)