React Native 可以渲染網(wǎng)絡(luò)圖片,也可以渲染本地圖片。
渲染網(wǎng)絡(luò)圖片
渲染網(wǎng)絡(luò)圖片的方式相對(duì)比較簡(jiǎn)單,通過uri就可以渲染
<Image style={styles.logo}
source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}} />
渲染本地圖片
渲染本地圖片的方式分為兩步:
- 首先需要在原生平臺(tái)的開發(fā)工具中加入靜態(tài)圖片資源:
IOS平臺(tái)需要在xcode中引入靜態(tài)圖片1x、2x、3x;
Android平臺(tái)需要在src\main\res\drawable中加入圖片資源。 - 然后通過
require(image!name)的方式進(jìn)行引入。
name是指圖片資源的名字,不包括擴(kuò)展名!
<Image style={styles.icon} source={require('image!myIcon')} />
如:我在資源文件中加入了a.jpg

Paste_Image.png
我要在使用它,可以這樣:
<Image source={require('image!a')} style={styles.image}></Image>
效果如下:

Paste_Image.png
參考文檔:https://facebook.github.io/react-native/docs/image.html#adding-static-resources-to-your-android-app