I needed to do that some time ago, so I wrote a component. What I'm basically doing here is comparing the screen ratio with the image ratio, and setting the width and height accordingly using a simple formula.
const Photo = ({imageUri}) => {
const [imageSize, setImageSize] = useState({width: 0, height: 0});
useEffect(() => {
Image.getSize(imageUri, (imageWidth, imageHeight) => {
const screenWidth = Dimensions.get('window').width;
const screenHeight = Dimensions.get('window').height;
if (imageWidth / imageHeight < screenWidth / screenHeight) {
setImageSize({
width: imageWidth + (imageWidth * (screenHeight - imageHeight)) / imageHeight,
height: screenHeight,
});
} else {
setImageSize({
height: imageHeight + (imageHeight * (screenWidth - imageWidth)) / imageWidth,
width: screenWidth,
});
}
});
}, []);
return (
<View style={styles.container}>
<Image style={imageSize} source={{uri: imageUri}} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'black',
},
});
export default Photo;
And there you go, no matter what your image ratio or screen ratio is, the image will fit in the screen.
All though keep in mind that if the user rotates the screen, this will not be calculated again, so if you have an app that has portrait and landscape mode enabled, you have to do the calculation again on screen orientation change.