As per @martinarroyo's comment I added contentContainerStyle={{flex:1}}
to the ScrollView, like so:
<ScrollView contentContainerStyle={{flex:1}}>
<Image resizeMode="contain" style={{flex: 1, width: undefined, height: undefined}} source={this.props.images.infoImage} />
</ScrollView
This completely resolved my issue. The image is the full width of the display while maintaining the correct aspect ratio.
EDIT: Turns out the image frame height stays the same but the image scales down. This means it has large chunks of padding on the top and bottom. As of now I don't know how to remove it.
EDIT 2:
It seems there aren't any tools out of the box with RN and ultimately ended up using a modified version of @Ihor Burlachenko's solution. I created a custom component that takes desired width/height constraints and scales it based on that. I need to use this for local files, which Image.getSize()
method does not work on, so I modified Ihor's solution to allow for that by passing in a dimensions object that contain width and height.
import React from 'react';
import { Image } from 'react-native';
export default class ScalableImage extends React.Component {
constructor(props) {
super(props);
this.state = {
width: null,
height: null,
}
}
componentDidMount() {
if(typeof this.props.source === 'string') {
Image.getSize(this.props.source, this._calculateImageDimensions, console.log);
} else if(this.props.dimensions) {
this._calculateImageDimensions(this.props.dimensions.width, this.props.dimensions.height)
}
}
render() {
return (
<Image
{ ...this.props }
style={[
this.props.style,
{ width: this.state.width, height: this.state.height }
]}
/>
);
}
_calculateImageDimensions(width, height) {
let ratio;
if (this.props.width && this.props.height) {
ratio = Math.min(this.props.width / width, this.props.height / height);
}
else if (this.props.width) {
ratio = this.props.width / width;
}
else if (this.props.height) {
ratio = this.props.height / height;
}
this.setState({ width: width * ratio, height: height * ratio });
}
}
ScalableImage.propTypes = {
width: React.PropTypes.number,
height: React.PropTypes.number,
dimensions: React.PropTypes.object
};
I then use it as so:
<ScalableImage source={require('../path/to/local/image.png')}
width={Dimensions.get('window').width()}
dimensions={{width: 700, height: 400}} />