我已经解决了这个问题,但在互联网上进行了几次搜索后,我使用componentDidMount解决了它
React-navigation 在选项卡之间切换时不会卸载组件。因此,当您离开并返回带有相机组件的屏幕时,它将只是黑色视图。所以一个好的解决方案是使用componentDidMount 并添加两个监听器willFocus 和willBlur 来帮助您挂载和卸载视图。
componentDidMount() {
const { navigation } = this.props;
navigation.addListener('willFocus', () =>
this.setState({ focusedScreen: true })
);
navigation.addListener('willBlur', () =>
this.setState({ focusedScreen: false })
);
}
render() {
const { isPermitted, focusedScreen } = this.state;
if (isPermitted === null) {
return (<View style={{flex:1, justifyContent:'center',alignItems:'center'}}>
<Text>Error</Text>
</View>);
} else if (isPermitted === false) {
return (<View style={{flex:1, justifyContent:'center',alignItems:'center'}}>
<ActivityIndicator size="large" style={{color:'blue', fontSize:30}} />
<Text>No Acceas to camera</Text>
</View>);
} else if (focusedScreen){
return ( <View>
<FullPageLoader isVisible={this.state.isLoader} TextToShow={this.state.loaderText} />
<CameraKitCameraScreen
actions={{ leftButtonText: <Icon name='close' style={{color:'white',fontSize:30}}/>,rightButtonText: <Icon name='images' style={{color:'white',fontSize:40}}/> }}
onBottomButtonPressed={event => this.onBottomButtonPressed(event)}
ref= {(cam) => {
this.camera = cam;
}}
flashImages={{
on: require('./img/flashon.png'),
off: require('./img/flashoff.png'),
auto: require('./img/flashauto.png'),
}}
TopTitle={this.state.title}
cameraFlipImage={require('./img/flip-camera.png')}
captureButtonImage={require('./img/capture.png')}
cameraOptions={{
flashMode: 'auto', // on/off/auto(default)
focusMode: 'on', // off/on(default)
zoomMode: 'on', // off/on(default)
ratioOverlayColor: '#00000077'
}}
style={{height:'100%'}}
/>
</View>);
}else{
return (<Text>Camera Error</Text>);
}
}
参考:https://github.com/react-native-community/react-native-camera/blob/master/docs/react-navigation.md