在React Native中显示SVG图像的问题

huangapple go评论66阅读模式
英文:

Problem displaying SVG images in React Native

问题

我在我的React Native应用中显示SVG图像时遇到了问题。当我尝试在React Native应用中显示它时,它会变形,不如预期那样看起来。

以下是图像应该显示的方式:

在React Native中显示SVG图像的问题

而在我的React Native应用中看起来是这样的:

在React Native中显示SVG图像的问题

我已经包含了我正在使用的SVG图像文件:[文件附件]
为了显示图像,我尝试了以下两种方法:

import AltoDark from '@/assets/image/profil/alto_dark.svg';
...
<View>
//第一种方法
<AltoDark/>
// 第二种方法
<SvgXml xml={AltoDark} />
</View>

我的package.json如下:

 "react-native-svg": "10.1.0",
 "react-native-svg-transformer": "^1.0.0",

我的Metro配置如下:

const { getDefaultConfig } = require('metro-config');

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts },
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve('react-native-svg-transformer'),
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== 'svg'),
      sourceExts: [...sourceExts, 'svg'],
    },
  };
})();

SVG文件可以在此处找到:https://pastebin.com/kPW2Qr32。有人能建议是什么原因导致了这个问题,以及我如何修复它,使图像在我的React Native应用中正确显示?

英文:

I'm having trouble displaying an SVG image in my React Native app. When I try to display it in my React Native app, it gets distorted and doesn't look as expected.

Here is how the image should appears:

在React Native中显示SVG图像的问题

And here is how it looks in my React Native app:

在React Native中显示SVG图像的问题

I've included the SVG image file that I'm using here:


To display the image I tried the following both method:

import AltoDark from &#39;@/assets/image/profil/alto_dark.svg&#39;;
...
&lt;View&gt;
//first method
&lt;AltoDark/&gt;
// second method
&lt;SvgXml xml={AltoDark} /&gt;
&lt;/View&gt;

my package.json is the following one

 &quot;react-native-svg&quot;: &quot;10.1.0&quot;,
 &quot;react-native-svg-transformer&quot;: &quot;^1.0.0&quot;,

and my metro config is

const {getDefaultConfig} = require(&#39;metro-config&#39;);

module.exports = (async () =&gt; {
  const {
    resolver: {sourceExts, assetExts},
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve(&#39;react-native-svg-transformer&#39;),
    },
    resolver: {
      assetExts: assetExts.filter(ext =&gt; ext !== &#39;svg&#39;),
      sourceExts: [...sourceExts, &#39;svg&#39;],
    },
  };
})();

The svg file can be found here: https://pastebin.com/kPW2Qr32. Anyone suggest what might be causing this issue and how I can fix it so that the image looks correct in my React Native app?

答案1

得分: 0

在我的SVG文件中,我更改了"objectBoundingBox"尺寸。从1x1变为2x2。然后,我调整了"transform="matrix()""中倒数第二个参数以调整图像的水平位置。从0.160427变为0.760427

英文:

In my svg file i change the objectBoundingBox dimension. I went from 1x1 to 2x2. Then I played with the before last argument of the transform="matrix()" to adjust the horizontal position of the image. I went from 0.160427to 0.760427

&lt;pattern id=&quot;pattern0&quot; patternContentUnits=&quot;objectBoundingBox&quot; width=&quot;1&quot; height=&quot;1&quot;&gt;
&lt;use xlink:href=&quot;#image0_595_4081&quot; transform=&quot;matrix(0.00191705 0 0 0.000715206 0.160427 0.0851681)&quot;/&gt;
&lt;/pattern&gt;
&lt;pattern id=&quot;pattern0&quot; patternContentUnits=&quot;objectBoundingBox&quot; width=&quot;2&quot; height=&quot;2&quot;&gt;
&lt;use xlink:href=&quot;#image0_595_4081&quot; transform=&quot;matrix(0.00191705 0 0 0.000715206 0.760427 0.0851681)&quot;/&gt;
&lt;/pattern&gt;

答案2

得分: 0

  1. 这里将SVG转换为JSX元素。
  2. 创建一个.tsx文件并粘贴JSX元素。
  3. 给它一个合适的名称,并将JSX元素导出为一个函数组件。
  4. 在你的组件中像这样使用它。
import AltoDark from '../../components';

...

<AltoDark/>
英文:

I follow the below steps while using an SVG image in React Native app.

  1. Convert SVG to JSX element from here.
  2. Create a .tsx file and paste the JSX element.
  3. Give a desirable name & export the JSX element as a functional component.
  4. Use it like this in your component.
import AltoDark from &#39;../../components&#39;;

...
...


&lt;AltoDark/&gt; 

huangapple
  • 本文由 发表于 2023年2月10日 12:59:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定