Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文件中响应本机显示SVG_Svg_React Native - Fatal编程技术网

从文件中响应本机显示SVG

从文件中响应本机显示SVG,svg,react-native,Svg,React Native,如何在React Native中显示我需要的SVG图像() 未在积极开发中 无法确定如何加载文件 WebView(或桥接WebView)无法确定如何加载所需的文件 显示图像的最佳方式是什么(不需要操纵它) 或者我应该把它转换成png吗?转换成png是最简单的解决方案。SVG图像被列为一个难点,如果您认为有必要,您可以对其进行升级,但目前还没有适合SVG图像的现成解决方案。您可以使用webview解决这个问题。我还必须将我的SVG作为字符串放入.js文件中(这一点都不好)。但它的工作相当可靠。

如何在React Native中显示我需要的SVG图像()

  • 未在积极开发中
  • 无法确定如何加载文件
  • WebView(或桥接WebView)无法确定如何加载所需的文件
显示图像的最佳方式是什么(不需要操纵它)


或者我应该把它转换成png吗?

转换成png是最简单的解决方案。SVG图像被列为一个难点,如果您认为有必要,您可以对其进行升级,但目前还没有适合SVG图像的现成解决方案。

您可以使用webview解决这个问题。我还必须将我的SVG作为字符串放入.js文件中(这一点都不好)。但它的工作相当可靠。在SVG加载之前,有一瞬间它们显示为白色,但这对我来说已经足够好了

大致如下:

import React, { View, WebView } from 'react-native'
import s from 'string'

const firstHtml = '<html><head><style>html, body { margin:0; padding:0; overflow:hidden } svg { position:fixed; top:0; left:0; height:100%; width:100% }</style></head><body>'
const lastHtml = '</body></html>'
export default (props) =>
  <View style={props.containerStyle}>
    <WebView
      style={[{ width: 200, height: 100, backgroundColor: props.backgroundColor }, normalizeStyle(props.style)]}
      scrollEnabled={false}
      source={{ html: `${firstHtml}${props.svgContent}${lastHtml}` }}
    />
  </View>
import React,{View,WebView}来自“React native”
从“字符串”导入
const firstHtml='html,正文{margin:0;padding:0;溢出:hidden}svg{position:fixed;top:0;left:0;高度:100%;宽度:100%}'
常量lastHtml=''
导出默认值(道具)=>

您可以使用此库将小SVG文件显示为数据

注意:大型SVG图像无法正确渲染

npm i react本机远程svg

从“react native remote svg”导入图像
如果您想使用上述库远程显示任意大小的图像,然后执行此操作

<Image
  source={{ uri: 'https://example.com/my-pic.svg' }}
  style={{ width: 200, height: 532}}
/>

但是如果您使用这个来显示本地svg文件,那么当您捆绑并发布该版本时,android中会出现一些问题

import Image from 'react-native-remote-svg'
<Image source={require('./image.svg')} />
从“react native remote svg”导入图像
或者您可以使用Icomoon(用于小型SVG图像/图标)

推荐使用:将Icomoon用于图标和小型SVG图像


请参见此以在react native中设置icomoon

谢谢!我已经在
react native remote svg
上为此发布了一个npm软件包,该软件包可能与“请勿使用此插件”
react native remote svg
重复,因为它在Android生产模式下存在严重缺陷。“不过iOS还不错。”@somnathbm是的。但是这个bug只适用于本地svg图像。如果您使用远程SVG,那么它可以正常工作。
import Image from 'react-native-remote-svg'
<Image source={require('./image.svg')} />