【发布时间】:2020-07-16 06:36:29
【问题描述】:
在我的 RN 0.62.2 应用程序中,一个函数用于显示从图库中挑选的本地图像:
const displayImg = (img_source, width, ht, local=false) => {
if (local) {
return (<Image source={require(img_source)} style={{width:width, height:ht, resizeMode:'cover'}}/>); //<<<== require causes error with dynamic image source
} else { //online
return (<FastImage source={{uri:img_source}} resizeMode={FastImage.resizeMode.cover} style={{width:width, height:ht}}/>);
};
};
但是由于捆绑时图片源不可用,RN抛出error为require。上面这个函数的目的是通过拾取的图像数量来动态排列图像显示格式。例如,对于 1 张图像,显示的图像将采用全宽。如果有 2 张图像,则一张图像将与另一张图像并排占据一半宽度。由于应用程序在用户选择 miage 之前不知道要显示哪个图像,所以 require(image_source) 在捆绑时不会知道确切的路径,这违反了 React Native 捆绑。有没有办法解决这个问题?
【问题讨论】:
-
不要对用户输入图像使用 require。只需使用 {uri: img_source} 因为 react-native-image-picker 将返回拾取图像的 uri
-
bravemaster,感谢您提供的信息。我认为require必须用于本地图像。是的,图像选择器确实会返回所选择的本地图像的路径。
标签: react-native react-native-image-picker