如何在React Native中推动本地图像?

发布于 2025-02-10 14:42:33 字数 2022 浏览 1 评论 0原文

我有一系列本地图像,我需要将这些图像仅将这些图像推入与类别ID匹配的另一个数组。我能够将图像添加到数组中,但是给出正确的路径,它是给出445

的数字,这是我的图像数组,

 pieImages: [
      require('../assets/images/otherbcateWhite.png'),
      require('../assets/images/non_alcoholicWhite.png'),
      require('../assets/images/alcoholicWhite.png'),
      require('../assets/images/babyFoodWhite.png'),
      require('../assets/images/bakedWhite.png'),
      require('../assets/images/foodWhite.png'),
      require('../assets/images/frozenWhite.png'),
      require('../assets/images/fruitsWhite.png'),
      require('../assets/images/houseHoldWhite.png'),
      require('../assets/images/meatFishWhite.png'),
      require('../assets/images/dairyProductsWhite.png'),
      require('../assets/images/sweetsWhite.png'),
      require('../assets/images/bodyWhite.png'),
      require('../assets/images/hotWhite.png'),
    ]

我的功能是我的功能以比较ID并将其推在另一个数组中

  if (item?.id === itemm?.categoryId) {
              budgetPipeData.Images.pieImages.map((_, index) => {
                if (index === parseInt(itemm.categoryId)) {
                  const img =
                    budgetPipeData.Images.images[parseInt(itemm.categoryId)];
                  console.log(img);
                  images.push(img);
                }
              });
    }

,并且如果我使用简单的数组字符串,则 图像

pieImages: [
      '../assets/images/otherbcateWhite.png',
    ]

诸如它给出错误的

Could not find image file:///Users/netzwelt/Library/Developer/CoreSimulator/Devices/344CC058-AD85-4595-9795-25A06A8CD235/data/Containers/Bundle/Application/A8637CD2-7511-4F4F-BBD1-73900FC60914/BoilerPlate.app/../163.png

,如果我使用需要在内部图像组件(例如

<Image source={require(images[index])} />

给出错误)中

[Error: TransformError src/components/charts/labels.tsx: src/components/charts/labels.tsx:Invalid call at line 50: require(images[index])]

,谁能告诉我如何正确推动图像?

I have an array of local images I need to push only those images into another array that matches category id. I able to add the images into array but instead giving proper path it's giving number like 445

that's my array of images

 pieImages: [
      require('../assets/images/otherbcateWhite.png'),
      require('../assets/images/non_alcoholicWhite.png'),
      require('../assets/images/alcoholicWhite.png'),
      require('../assets/images/babyFoodWhite.png'),
      require('../assets/images/bakedWhite.png'),
      require('../assets/images/foodWhite.png'),
      require('../assets/images/frozenWhite.png'),
      require('../assets/images/fruitsWhite.png'),
      require('../assets/images/houseHoldWhite.png'),
      require('../assets/images/meatFishWhite.png'),
      require('../assets/images/dairyProductsWhite.png'),
      require('../assets/images/sweetsWhite.png'),
      require('../assets/images/bodyWhite.png'),
      require('../assets/images/hotWhite.png'),
    ]

my function to compare id and push it in another array

  if (item?.id === itemm?.categoryId) {
              budgetPipeData.Images.pieImages.map((_, index) => {
                if (index === parseInt(itemm.categoryId)) {
                  const img =
                    budgetPipeData.Images.images[parseInt(itemm.categoryId)];
                  console.log(img);
                  images.push(img);
                }
              });
    }

and if I use simple string of array for images like

pieImages: [
      '../assets/images/otherbcateWhite.png',
    ]

it's giving error

Could not find image file:///Users/netzwelt/Library/Developer/CoreSimulator/Devices/344CC058-AD85-4595-9795-25A06A8CD235/data/Containers/Bundle/Application/A8637CD2-7511-4F4F-BBD1-73900FC60914/BoilerPlate.app/../163.png

and if I use require inside Image component like

<Image source={require(images[index])} />

it's give error

[Error: TransformError src/components/charts/labels.tsx: src/components/charts/labels.tsx:Invalid call at line 50: require(images[index])]

can anyone tell me how can push the images properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2025-02-17 14:42:33

理想情况下,这是正确的,因为require()将在该路径上为您提供资源ID,并且您可以立即直接将其用作image源。

<Image source={images[0]} />

但是,如果您希望它用于进一步使用,例如上传到服务器,则可以将字符串存储在数组中,而不是需要

pieImagesString: [
  '../assets/images/otherbcateWhite.png',
  '../assets/images/non_alcoholicWhite.png'
  ...
]

Ideally, that is correct, because require() will provide you id of resource at that path and you can directly use it now as Image source.

<Image source={images[0]} />

However, if you want it to use for further use like upload to server, you can store string in an array instead of require

pieImagesString: [
  '../assets/images/otherbcateWhite.png',
  '../assets/images/non_alcoholicWhite.png'
  ...
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文