我正在开发一款名为Reaction Native EXPO的应用程序,但遇到一条警告,指出它在特定路径下找不到图像文件.警告消息提到以下路径:

WARN Could not find image file:///Users/[USERNAME]/Library/Developer/CoreSimulator/Devices/F8428037-7C3C-4084-8236-2357D1AF4D42/data/Containers/Bundle/Application/5C0D625D-94E4-465A-9613-405D666B4E79/Exponent-2.28.9.tar.app/./assets/story2.jpg

代码:

    const stories = ['story1.jpg', 'story2.jpg', 'story3.jpg', 'story4.jpg', 'story1.jpg', 'story1.jpg'];

    <StoriesView stories={stories} />

    const StoriesView = ({ stories }) => {
  return (
    <ScrollView horizontal showsHorizontalScrollIndicator={false} style={styles.storiesScrollView}>
      <View style={styles.storiesContainer}>
        {stories.map((name, index) => (
          <Image
            key={index}
            style={styles.storyImage}
            source={{ uri: `./assets/${name}` }}
          />
        ))}
      </View>
    </ScrollView>
  );
};

我已经验证了路径并确保图像文件存在,但警告仍然存在.有人能就如何排除故障和解决此问题提供建议吗?我正在使用Reaction Native EXPO.

任何帮助或见解都将不胜感激.谢谢!

enter image description here

推荐答案

您在React Native中使用的是Expo,因此这意味着需要以与您使用的方法稍有不同的方式加载图像.您当前直接从相对路径加载图像文件的try 不起作用,因为bundler不知道要包括它们.

require function通常用于在Reaction Native中导入图像,因为它可以动态Bundle 这些资源.因此,您应该更改图像导入,如以下代码所示:

const stories = [
    require('./assets/story1.jpg'), 
    require('./assets/story2.jpg'), 
    require('./assets/story3.jpg'), 
    require('./assets/story4.jpg'), 
    require('./assets/story1.jpg'), 
    require('./assets/story1.jpg')
];

const StoriesView = ({ stories }) => {
    return (
        <ScrollView horizontal showsHorizontalScrollIndicator={false} style={styles.storiesScrollView}>
            <View style={styles.storiesContainer}>
                {stories.map((source, index) => (
                    <Image
                        key={index}
                        style={styles.storyImage}
                        source={source}
                    />
                ))}
            </View>
        </ScrollView>
    );
};

您可以看到require,用于从本地资源导入每个图像文件.然后,它们被直接传递给Image组件的sourceprops ,而不是使用字符串URI.


但要知道,这种方法将不能动态加载图像名称,因为在Bundle 时处理require条语句,应用程序已经开始运行before条.对文件路径进行判断,并将图像加载到应用程序的Bundle 包中,这意味着需要首先明确定义它们.

require函数不支持动态图像导入.在构建应用程序的Bundle 包时,绑定者需要知道它应该包括的所有assets资源 .如果使用像require('./assets/' + name)这样的动态表达式,则Bundle 器不知道要包括图像,因为它事先不知道name的值.

以下是你可能会做的一个例子:

import React, { Component } from 'react';
import { Image, StyleSheet, AppRegistry } from 'react-native';

const images = {
  story1: require('./assets/story1.jpg'),
  story2: require('./assets/story2.jpg'),
  story3: require('./assets/story3.jpg'),
  // add more images as needed
};

const ImageWithRequire = ({ name }) => {
  const image = images[name];

  return (
    <Image
      key={name}
      style={styles.storyImage}
      source={image}
    />
  );
};

const styles = StyleSheet.create({
  storyImage: {
    width: 100,
    height: 100,
  },
});

AppRegistry.registerComponent('ImageWithRequire', () => ImageWithRequire);

With const images, all possible images are required upfront and stored.
You can then use the name prop to select the right image from the images object. That allows you to determine the image to display at runtime while still including all possible images in the app's bundle.

示例:<ImageWithRequire name="story1" />

Javascript相关问答推荐

当在select中 Select 选项时,我必须禁用不匹配的 Select

使用axios.获取实时服务器时的404响应

react/redux中的formData在expressjs中返回未定义的req.params.id

被CSS优先级所迷惑

Rehype将hashtag呈现为URL

阿波罗返回的数据错误,但在网络判断器中是正确的

JSDoc创建并从另一个文件导入类型

不能将空字符串传递给cy.containes()

IF语句的计算结果与实际情况相反

对网格项目进行垂直排序不起作用

为什么我的getAsFile()方法返回空?

如何在ASP.NET项目中使用Google Chart API JavaScript将二次轴线值格式化为百分比

创建以键值对为有效负载的Redux Reducer时,基于键的类型检测

如何在Bootstrap中减少网格系统中单个div的宽度

将对象推送到数组会导致复制

如何在Java脚本中对列表中的特定元素进行排序?

在JS/TS中将复杂图形转换为数组或其他数据 struct

表单数据中未定义的数组键

为什么这个最小Angular 的Licial.dev设置不起作用?

React数组查找不读取变量