docs
你可以用两种方法来做。
使用静态图像,但需要对JSON进行一些更改。像这样重写你的对象
{
"addr": "124 test drive, Ring road",
"phone": "(951)-955-6200",
"LatL":"33.977880",
"Long2":"-117.373423",
"cLat": "33.931989",
"cLong": "-117.409222",
"Online": "https://www.test.org",
"image" : require("CAC.png") // <-- This line
}
像这样使用
<Image source={item.image} style = {styles.imageView}/>
第二:
如果您从外部服务器获取这个JSON,那么您也应该从服务器获取图像。
{
"addr": "124 test drive, Ring road",
"phone": "(951)-955-6200",
"LatL":"33.977880",
"Long2":"-117.373423",
"cLat": "33.931989",
"cLong": "-117.409222",
"Online": "https://www.test.org",
"image" : "https://example.com/path/to/your/image.png" // <-- This line
}
<Image source={{uri: item.image}} style = {styles.imageView}/>
更新:
对于这个问题,你可以这样做。
{
"addr": "124 test drive, Ring road",
"phone": "(951)-955-6200",
"LatL":"33.977880",
"Long2":"-117.373423",
"cLat": "33.931989",
"cLong": "-117.409222",
"Online": "https://www.test.org",
"image" : "CAD" // <-- This line
}
以及功能
getImage = (image) => {
switch (image) {
case "CAD":
return require("CAD.png")
break;
case "CAD2":
return require("CAD2.png")
break;
case "CAD3":
return require("CAD3.png")
break;
default:
return require("CAD4.png");
break;
}
}
以及
<Image source={this.getImage(item.image)} style = {styles.imageView}/>