我正在尝试在SVG
应用上渲染React Native
图像。
由于无法使用来自SVG
包的标准Image
组件渲染react-native
图像,因此我尝试了最流行的替代方案:Image
组件从包装中:react-native-remote-svg
。
我通过手机上的Expo
应用尝试了以下代码,但没有成功,在我的Expo
云帐户上运行了已构建的应用(在从本地计算机到我的{{1 }}帐户,并输入以下命令:Expo
)。我在两种$ expo ba
和Android
设备上都尝试过。
iOS
通过这种方式,即使您在import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';
import Image from 'react-native-remote-svg';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
Please, change extension from{"\n"}
PNG to SVG{"\n"}
The image will stop rendering.{"\n"}
Why? Workaround?
</Text>
<View style={styles.containerHomer}>
<Image
source={require('./assets/homer.png')}
style={{ width: 120, height: 128 }}
/>
</View>
<Card>
<AssetExample />
</Card>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
containerHomer: {
flexDirection: 'row',
justifyContent: 'center',
marginBottom: 20,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});
在线游乐场上尝试上面的代码,您也会看到Snack
图像没有被渲染,而SVG
图像被渲染了正确(您可以尝试将扩展名从PNG
更改为SVG
)。
在这里,您可以使用PNG
样本:
https://snack.expo.io/r1kWxxCeN
另一方面,如果我通过设备上的Snack
应用程序运行该应用程序,但是这次从url:Expo
加载,则exp://<IP>:19000
会正确呈现。我相信是因为在那种情况下,图像是使用HTML内部呈现的(但我不确定)。
关于如何在SVG
应用中正确渲染SVG
图像的任何想法?
谢谢!