在 React Native 中需要图像模块时遇到问题

Trouble requiring image module in 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);

主要故障区域是:

    <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 调用能够找到该图像并为此添加了一个图像集:

...但无济于事。有人有什么想法吗?我已经阅读了文档并且 似乎 正在按照规定的方式执行此操作。谢谢!

您运行正在使用哪个版本的 React Native?与此相关的打包程序存在问题,已解决。

如果是这种情况,您可以更新或 运行 开发服务器:

node_modules/react-native/packager/packager.sh --assetRoots path/to/Images.xcassets

https://github.com/facebook/react-native/issues/282

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

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

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)

还要确保重新启动打包程序。这为我解决了这个问题。

自 React Native 发布 0.14 以来,iOS 和 Android 的图像处理已经标准化,现在有所不同。除了关于 Github 的非常清晰的提交说明外,我找不到任何文档:Document new asset system.

只有建议文档的屏幕截图:

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

这个问题已经得到解答,但是如果你在 Android 上使用 React Native,你可能会遇到同样的问题。对我来说,解决方案是使用 source={{ uri: "google", isStatic: true }} 而不是 source={required('./bg.png')}.

只是不要忘记将您的图像添加到 src/main/res/drawable 文件夹中。

您可以通过在资产文件夹中创建 package.json 文件来轻松引用图像。

Project folder structure

package.json

并且您可以通过此代码调用它。

<Image 
   source={require('assets/img/avatar.png')} 
   style={styles.itemAvatar}
   resizeMode={'cover'}
/>

对于静态图片,您需要提供如下路径:

<Image source={require('./images/back.png')}  
style= {{width:25, height:25, marginLeft:12, padding:12}}/>

这对我有用,因为图像存储在项目目录的图像文件夹中: