将图片文件 UePbdph.png 放入与index.android.js的同目录中,在index.android.js中引入:
<Image source={require('./UePbdph.png')} style={styles.thumbnail}/>
环境:
react版本:15.0.2
react-nativeb版本:0.26.2
react-native-cli版本:0.2.0
打开 ode_modules eact-nativeLibrariesImageAssetSourceResolver.js文件,将原文:
function getScaledAssetPath(asset): string { var scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get()); var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; var assetDir = assetPathUtils.getBasePath(asset); return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; }
改为:
function getScaledAssetPath(asset) { var scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get()); var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; var assetDir = assetPathUtils.getBasePath(asset); //return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; var imagePath = assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; if (imagePath[0] == "/" || imagePath[0] == "\") { imagePath = imagePath.substr(1); } return imagePath; }
问题:
1. 如果把图片放入文件夹中,比如在根目录新建一个img文件夹,把图片放进去,引入方式改为:
<Image source={require('./img/UePbdph.png')} style={styles.thumbnail}/>
会报错,不知道为什么?
2.如果放在根目录中,打包成app,应该放在哪里呢?
以上问题,还需继续研究回答,也烦请知道的同志们帮忙回答,tks!
原文引用:http://blog.csdn.net/sbsujjbcy/article/details/49981529