javascript - react-native - Image require local path from JSON - Stack Overflow

Hello unity :) I am working on a testapp in react-native and try to get Images from a local storage pla

Hello unity :) I am working on a testapp in react-native and try to get Images from a local storage place. Actual what i am doing: I give an image directlink source to a var and call this method at my render function.

*react: ^0.14.8 ||*
*react-native: ^0.23.1||*
*npm: 2.15.1 ||*
*node: v4.4.4*

Working:

ImageCall-File (Imagefile.js)

var samplepicture;

setPicture(){
    samplepicture= require('./images/picture.png')
}
render() { 
    this.setPicture; 
    return(
        <Image source=samplepicture/>
    ); 
}

The code snippet above works in a created class wich is named after (Imagefile.js)

The method above works pretty fine.

Now my where i want to get to > I want to save the image file in a seperated js file wich is styled like a JSON object.

Not working:

JS_JSON_File (pictureRef.js)

module.exports = {
    "pictureRef": 
{
    "picture": './images/picture.png'
}}

ImageCall_File (Imagefile.js)

var pictureRef = require('./pictureRef.js');
var samplepicture;

setPicture(){
    samplepicture= require(pictureRef.pictureRef.picture);
}
render() { 
    this.setPicture; 
    return(
        <Image source=samplepicture/>
    ); 
}

Debug the samplepicture: samplepicture = ./images/picture.png

Not sure if it is relevant but because it is a string it is formatted without the ' '. Also keep in mind that i need to set the image source by the setPicture() function for my code.

*I debugged both versions of code and had the same output:

  • Version 1: samplepicture = ./images/picture.png
  • Version 2: samplepicture = ./images/picture.png

*

Please keep in mind that this is my first question and i really hoped to format everything right. Also i searched for many related issue´s and didnt find any solution

Thanks for your help.

Hello unity :) I am working on a testapp in react-native and try to get Images from a local storage place. Actual what i am doing: I give an image directlink source to a var and call this method at my render function.

*react: ^0.14.8 ||*
*react-native: ^0.23.1||*
*npm: 2.15.1 ||*
*node: v4.4.4*

Working:

ImageCall-File (Imagefile.js)

var samplepicture;

setPicture(){
    samplepicture= require('./images/picture.png')
}
render() { 
    this.setPicture; 
    return(
        <Image source=samplepicture/>
    ); 
}

The code snippet above works in a created class wich is named after (Imagefile.js)

The method above works pretty fine.

Now my where i want to get to > I want to save the image file in a seperated js file wich is styled like a JSON object.

Not working:

JS_JSON_File (pictureRef.js)

module.exports = {
    "pictureRef": 
{
    "picture": './images/picture.png'
}}

ImageCall_File (Imagefile.js)

var pictureRef = require('./pictureRef.js');
var samplepicture;

setPicture(){
    samplepicture= require(pictureRef.pictureRef.picture);
}
render() { 
    this.setPicture; 
    return(
        <Image source=samplepicture/>
    ); 
}

Debug the samplepicture: samplepicture = ./images/picture.png

Not sure if it is relevant but because it is a string it is formatted without the ' '. Also keep in mind that i need to set the image source by the setPicture() function for my code.

*I debugged both versions of code and had the same output:

  • Version 1: samplepicture = ./images/picture.png
  • Version 2: samplepicture = ./images/picture.png

*

Please keep in mind that this is my first question and i really hoped to format everything right. Also i searched for many related issue´s and didnt find any solution

Thanks for your help.

Share Improve this question asked Jun 3, 2016 at 7:35 Jonathan StellwagJonathan Stellwag 4,2874 gold badges28 silver badges56 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

seems it similar with this issue1 issue2

i think if you use require() to add your image,you should give a static path,not a variable. because they need know the specific image source to bundle. use json is just like a uncertain source so it doesnt work now there is a pr to this,

The best solution I found today is that no solution for object - variable for dynamically loading from json file in react-native, but have to convert that image to base64 format and add it to json file, after that dispatch it from there by normal.

export class AppModule { }
function _loadDataSoundFromJson(){ 
  return new Promise((resolve,reject)=>{ 
    if (soundData){ 
      setTimeout(function(){ 
        resolve(soundData) },1500) 
    }
	else { 
      reject("No data found") 
    }}); 
};

_renderNoPlaying = (item) => {
   return ( 
     <View style={styles.horizontalContainer}> 
     <Image 
       	source={{uri : item.img!=null?item.img:nullImg.nil}} 
       	resizeMethod={'scale'} 
     	style={{
     		width:150,
     		height:120, 
     		borderTopLeftRadius:20,
     		borderTopRightRadius:20}}
     /> 
     <Text style={styles.titleItem}>{item.key}</Text> 
)}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744963303a4603518.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信