Skip to content

Instantly share code, notes, and snippets.

@zi6xuan
Last active May 22, 2019 07:55
Show Gist options
  • Save zi6xuan/1e569ce3754d20f12143a893c558f5c5 to your computer and use it in GitHub Desktop.
Save zi6xuan/1e569ce3754d20f12143a893c558f5c5 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { Image, InteractionManager } from 'react-native';
export default class JImage extends Component<Props> {
constructor(props) {
super(props)
this.state = {
width: 1,
height: 1
}
}
componentDidMount() {
this.props.source && Image.getSize(this.props.source.uri, (w, h) => {
let { style } = this.props;
let width = 1;
let height = 1;
if (!style || !(style.width || style.height)) {
width = w;
height = h;
} else if (style.width) {
width = style.width;
height = (width * h) / w;
} else if (style.height) {
height = style.height;
width = (height * w) / h;
} else {
width = style.width;
height = style.height;
}
this.setState({ width: width, height: height });
});
}
render() {
let { style } = this.props;
let width = style && style.width ? style.width : this.state.width;
let height = style && style.height ? style.height : this.state.height;
return (
<Image {...this.props} style={[style, { width: width, height: height }]} resizeMode='stretch' />
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment