使用 <图像>使用本地文件 [英] Use &lt;Image&gt; with a local file

查看:23
本文介绍了使用 <图像>使用本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说引用静态图像的唯一方法是使用 require.

The documentation says that the only way to reference a static image is to use require.

但我不确定 react 期望在何处获得这些图像.这些示例没有任何域,因此您似乎必须转到 Xcode 并将它们添加到 Images.xcassets,但这对我不起作用.

But I'm not sure where does react expect to have those images. The examples don't have any domain, so it seems like you have to go to Xcode and add them to Images.xcassets, but that didn't work for me.

推荐答案

使用 React Native 0.41(2017 年 3 月),针对 iOS,我发现它很简单:

Using React Native 0.41 (in March 2017), targeting iOS, I just found it as easy as:

<Image source={require('./myimage.png')} />

图像文件必须与需要它的 .js 文件位于同一文件夹中.

The image file must exist in the same folder as the .js file requiring it.

我无需更改 XCode 项目中的任何内容.它只是有效.也许 2 年时间发生了很大的变化!

I didn't have to change anything in the XCode project. It just worked. Maybe things have changed a lot in 2 years!

请注意,如果文件名不是小写字母,或者路径不是./",那么对我来说,它开始失败.不确定限​​制是什么,但要从简单开始,然后继续前进.

Note that if the filename has anything other than lower-case letters, or the path is anything more than "./", then for me, it started failing. Not sure what the restrictions are, but start simple and work forward.

希望这对某人有所帮助,因为这里的许多其他答案似乎过于复杂且充满了(顽皮的)站外链接.

Hope this helps someone, as many other answers here seem overly complex and full of (naughty) off-site links.

更新:顺便说一句 - 官方文档在这里:https://facebook.github.io/react-native/docs/images.html

UPDATE: BTW - The official documentation for this is here: https://facebook.github.io/react-native/docs/images.html

这篇关于使用 <图像>使用本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆