gpt4 book ai didi

react-native - 将顶部与图像中的调整大小模式封面垂直对齐- react native

转载 作者:行者123 更新时间:2023-12-03 14:48:26 26 4
gpt4 key购买 nike

我有一个高度大于宽度的矩形图像。因此使用resizeMode ='cover'。它使纵横比保持相等,但从中心开始扩展,
看不到图像的顶部。我想做的是保持宽高比并从顶部显示图像。

请尝试小吃中的代码:https://snack.expo.io/@codebyte99/new-addition-to-home-page

代码:

<View>
<Image
source={{
uri:
'https://img.kpopmap.com/2017/09/HanYeSul-min.jpg',
}}
resizeMode={'cover'}
style={{ width: 120, height: 120 }}
/>
</View>

现在图片

enter image description here

形象我想要实现的目标

enter image description here

图片原图

enter image description here

我想从顶部对齐图像,以使头部清晰可见。我们可以在CSS中通过object-fit:cover和object-position:top做到这一点,但是如何在react native中完成呢?

最佳答案

您可以尝试使用ImageBackground和一些overflow hidden,如下所示:

import * as React from 'react';
import { Text, View, StyleSheet, ImageBackground } from 'react-native';
import Constants from 'expo-constants';


export default function App() {
return (
<View style={styles.container}>
<View styles={styles.imageContainer}>
<ImageBackground
source={{uri: '/image/n5xcc.jpg'}}
style={{
height: 200, // <-- you can adjust visible area
width: 200, // <-- same here
overflow: 'hidden'
}}
imageStyle={{
resizeMode: "cover",
height: 260, // <-- you can adjust this height to play with zoom
}}
>
</ImageBackground>
</View>
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: 'pink',
alignItems: 'center',
padding: 8,
},
});
结果应该是这样的:
enter image description here

关于react-native - 将顶部与图像中的调整大小模式封面垂直对齐- react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58524782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com