【问题标题】:Image is not fitting to full screen in iOS图像不适合在 iOS 中全屏显示
【发布时间】:2021-08-13 07:59:02
【问题描述】:

我正在使用react-native-image-viewer 来渲染图像,代码看起来像这样

   <View style={{flex: 1}}>
        <ImageViewer 
             imageUrls={images}
             renderImage={() => <Image style={{height: '100%', width: '100%'}} resizeMode={'contain'} source={{uri:`${axios.defaults.baseURL}${url}`, headers}} />}
             enableImageZoom={true}
             backgroundColor={TEXT_HEADERBLACK}
             maxOverflow={0}
             renderIndicator={() => <></>}
        />
    </View>

这段代码在iOS中的结果如下

如您所见,图像不适合整个屏幕。但在 android 中它占据了整个屏幕。

我是否进行了任何样式更改以使其占据整个屏幕?

【问题讨论】:

    标签: ios iphone image react-native


    【解决方案1】:

    运行以下代码作为零食,在我的设备上看起来不错。虽然我删除了renderImage 属性。

    import * as React from 'react';
    import { Text, View, StyleSheet, Image } from 'react-native';
    import ImageViewer from 'react-native-image-zoom-viewer';
    
    const images = [{
        // Simplest usage.
        url: 'https://avatars2.githubusercontent.com/u/7970947?v=3&s=460',
    
        // width: number
        // height: number
        // Optional, if you know the image size, you can set the optimization performance
    
        // You can pass props to <Image />.
        props: {
            // headers: ...
        }
    }, {
        url: 'https://avatars2.githubusercontent.com/u/7970947?v=3&s=460',
    }]
    // test
    
    export default function App() {
      return (
       <View style={{flex: 1}}>
            <ImageViewer 
                 imageUrls={images}
                 enableImageZoom={true}
                 backgroundColor={'#ffffff'}
                 maxOverflow={0}
                 renderIndicator={() => <></>}
            />
        </View>
      );
    }
    
    const styles = StyleSheet.create({
    
    });
    

    你可以链接你上面使用的图像吗?

    【讨论】:

    • 你在 iOS 中运行过这个 sn-p 吗?没有renderImage,我根本无法渲染图像。如果您能够在 iOS 中运行,请告诉我设备详细信息。我在 iPhone 6、iPhone SE(Old) 和 Simulator(iPhone Pro Max) 上试过
    • 我在 iPhone 10 XR 上运行它。我使用了最新版本的库。我将它作为零食运行,但通过snack.expo.io 作为快速测试。可能会有所帮助。
    • 我也在使用最新版本v3.0.1。但仍然没有renderImage 无法渲染图像。
    【解决方案2】:

    我对 iOS 上&lt;Image&gt; 的理解需要绝对值而不是相对值。您还可以根据您的应用运行的平台添加条件渲染。

    const {
      width: deviceWidth,
      height: deviceHeight,
    } = Dimensions.get('window');
    
    <Image
      style={{
        height: Platform.OS === 'ios' ? deviceHeight * 0.9 : '100%',
        width: Platform.OS === 'ios' ? deviceWidth * 0.9 : '100%'
      }}
      resizeMode={'contain'}
      source={{uri:`${axios.defaults.baseURL}${url}`, headers}}
    />
    

    【讨论】:

    • 它在提供完整的设备宽度和设备高度并将图像包装在&lt;View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}} /&gt;中后工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 2021-12-15
    • 2019-05-07
    • 2015-06-28
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多