Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: can't load image

I write a simple iOS program to show image with React Native.

'use strict';

var React = require('react-native');
var {
  Image
} = React;

var styles = React.StyleSheet.create({

  base: {
    height: 400,
    width: 400 
}
});

class SimpleApp extends React.Component {
  render() {
    return (
    <Image
    style={styles.base}
    source={require('image!k')}
    //source={{uri: 'http://news.xinhuanet.com/world/2015-05/09/127782089_14311512601821n.jpg'}}
      /> 
    )
  }
}

React.AppRegistry.registerComponent('SimpleApp', () => SimpleApp);

But I got the message from iPad screen:

"Failed to print error:","'undefined' is not an object (evaluating 'stackString.split')"

When I change the code to use image url

//source={require('image!k')}
source={{uri: 'http://news.xinhuanet.com/world/2015-05/09/127782089_14311512601821n.jpg'}}

I only get a red rect border.

When I use another js file, everything works well."Hello World" can show on iPad screen.

'use strict';

var React = require('react-native');
var {
  Text,

} = React;


class SimpleApp extends React.Component {
  render() {
    return (

        <Text>Hello World</Text>

    )
  }
}

React.AppRegistry.registerComponent('SimpleApp', () => SimpleApp);
like image 377
Han Pengbo Avatar asked Mar 17 '23 03:03

Han Pengbo


1 Answers

Make sure you are using a recent version of React Native. With React Native 0.4.4 following works:

Local image

  <Image
    style={styles.base}
    source={require('image!tabnav_settings')}
  />

Also make sure you added the image you want to use to your image assets:

Xcode Images.xcassets

Remote Image

  <Image
    style={styles.base}
    source={{uri: 'http://i.stack.imgur.com/DfkfD.png'}}
  />
like image 87
peter Avatar answered Mar 29 '23 06:03

peter