【问题标题】:react-native images with space in name not loading名称中有空格的反应本机图像未加载
【发布时间】:2021-11-13 20:04:39
【问题描述】:

我正在尝试在我的 react 本机应用程序中使用图像。

此时我的代码很少

这就是我现在拥有的所有代码

import React from 'react';
import { ScrollView, View, Text } from 'react-native';
import Image from '@src/assests/images/avatars/Allan Munger.png';
import { ImageCircle } from '@src/components/elements';

const App = () => {
  const [active, setActive] = React.useState(0);
  return (
    <View>
      <ImageCircle uri={Image} active={false} />
    </View>
  );
};

export default App;

ImageCircle 在哪里

import React from 'react';
import { View, Text, Image, ImageSourcePropType } from 'react-native';
import Styles from './styles';

const ImageCircle = ({ active, uri }) => {
  console.log('uri', uri);
  return (
    <View
      style={{
        width: 60,
        height: 60,
        borderRadius: 30,
        borderColor: 'red',
        borderWidth: 1,
      }}>
      <Image source={uri} width={60} height={60} />
    </View>
  );
};

export default ImageCircle;

我在圆圈中(或任何地方)看不到我的图像。

在控制台中,有这样一条日志

Asset not found: /Users/varunb/Desktop/project-emma/src/assests/images/avatars/Allan%20Munger@2x.png for platform: ios

谁能告诉我为什么我在 iPhone 上看不到图片以及为什么会出现此错误?以及如何解决?

Ps:如果我从图像导入中删除空间(并重命名图像),那么一切正常,即

import Image from '@src/assests/images/avatars/AllanMunger.png';

这是我在 iPhone 上看到的(导入时有空格)

【问题讨论】:

  • 看起来这可能是一个错误。有什么阻止您使用Allan_Munger.png 作为文件名吗?除此之外,我建议查看 react-native 的库代码并在 GitHub repository 上打开一个问题。
  • @Elias 它更像是一个编码任务。我不确定,但这可能是故意的,我的目标是找到解决此问题的方法
  • 我会去和分配者谈谈。我刚刚在本地环境中测试了带有空格的路径,并面临与您完全相同的问题。

标签: javascript reactjs typescript react-native


【解决方案1】:

希望这段代码对你有帮助

格式错误:

<Image source={uri} width={60} height={60} />

有两种渲染图像样式

<Image  style={styles.tinyLogo}
        source={require('@expo/snack-static/react-native-logo.png')}
      />
<Image  style={styles.tinyLogo}
        source={{
          uri: 'https://reactnative.dev/img/tiny_logo.png',
        }}
      />

你的情况

<Image source = {{uri: uri} width: {60} height:{60}}

如果还是不渲染图片,可以用FastImage代替Image

npm install react-native-fast-image 后,可以使用 FastImage

请查看此网址https://github.com/DylanVann/react-native-fast-image

它可以正常工作

【讨论】:

  • 不准确。我没有从外部来源加载图像。另外,我已经更新了我的问题。感谢您的回答:)
  • 我改了答案,请检查
  • 再次感谢。您答案中的代码 sn-p 不起作用,我不想使用 fast-image。
  • FastImage 绝对不需要。
  • 啊事实上这是不正确的。 @ViktorM 他们专门询问其中有空格的路径。您的不包含任何内容。
猜你喜欢
  • 2018-06-10
  • 2022-12-05
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多