开发中有市会遇到这种需求:把某个view生成图片进行分享。其实React Native有提供相应的API(目前仅适用iOS): https://github.com/facebook/react-native/commit/ac12f986899d8520527684438f76299675dc0daa.
View生成图片:
state = {
uri: null
};
takeToImage() {
UIManager.takeSnapshot(this.refs.location, {format: 'png', quality: 0.8}).then(
(uri) => this.setState({uri})
).catch(
(error) => alert(error)
);
}
render() {
return (<View>
<View ref='location'>
<Text>东经</Text>
<Text>东经</Text>
<Text>西经</Text>
<Text>东经</Text>
</View>
<Text onPress={()=>this.takeToImage()}>生成图片</Text>
<Image source={{uri: this.state.uri}} style={{borderWidth:1,height:50,width:300}}/>
</View>
)
}