【问题标题】:React Native:How to dynamically arrange local images in display when picked from local gallery?React Native:从本地图库中选择时如何动态排列显示的本地图像?
【发布时间】: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抛出errorrequire。上面这个函数的目的是通过拾取的图像数量来动态排列图像显示格式。例如,对于 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


【解决方案1】:

您不必要求用户输入图像。

您应该将它们用作外部图像,因为 react-native-image-picker 将返回拾取图像的 url。

所以不是

 <Image source={require(img_source)} />

你应该:

<Image source={{uri: img_source}} />

【讨论】:

  • 'bravemaster,对于'path : ', 'file:///data/user/0/com.xyz_app/cache/react-native-image-crop-picker/20160906_112918_HDR.jpg',它显示一个没有图像和错误的空框。删除了resizeMode,只留下样式,它是一样的。我还要更正什么?
猜你喜欢
  • 2019-04-11
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 2014-05-15
  • 2021-05-14
  • 1970-01-01
相关资源
最近更新 更多