重新显示在屏幕上时,本机相机冻结

时间:2020-03-31 08:49:16

标签: react-native react-native-navigation react-native-camera

我正在为学校项目开发一个应用程序,在这个应用程序中,每个视图中都有一个摄像头组件。当我回到视图时,相机冻结。

我的一个视图代码是:

class LoginView extends React.Component {
  constructor(props) {
    super(props);
  }

  togglePseudo = pseudo => {
    const action = {type: 'USER_PSEUDO', value: pseudo};
    this.props.dispatch(action);
  };

  render() {
    const {navigate} = this.props.navigation;
    const userPseudo = this.props.userPseudo;

    return (
      <View style={style.mainContainer}>
        <RNCamera
          style={style.backgroundCamera}
          type={RNCamera.Constants.Type.back}
          flashMode={RNCamera.Constants.FlashMode.on}
          androidCameraPermissionOptions={{
            title: "Permission d'utiliser la camera",
            message: "L'application necessite l'autorisation de la camera",
            buttonPositive: 'Autoriser',
            buttonNegative: 'Refuser',
          }}
        />
        <View style={style.goContainer}>
          <TouchableOpacity
            style={style.backButton}
            onPress={() => navigate('HomeView')}>
            <Image source={require('assets/images/back-button.png')} />
          </TouchableOpacity>
          <View style={style.centeredElements}>
            <Text style={style.titleMission}>Mission "Pc"</Text>
            <View style={style.modalChoosePseudo}>
              <Text style={style.modaltitle}>Choisir un pseudo</Text>
              <TextInput
                style={style.pseudoInput}
                onChangeText={pseudo => this.togglePseudo(pseudo)}
                value={userPseudo}
              />
              {userPseudo !== '' ? (
                <TouchableOpacity
                  style={style.validateButton}
                  onPress={() => navigate('InGame')}>
                  <Text style={style.buttonText}>Valider</Text>
                </TouchableOpacity>
              ) : (
                <TouchableOpacity
                  style={[style.validateButton, style.validateButtonDisabled]}
                  onPress={() =>
                    Alert.alert('Alerte', 'Veuillez entrer un pseudo')
                  }>
                  <Text
                    style={[style.buttonText, style.validateButtonDisabled]}>
                    Valider
                  </Text>
                </TouchableOpacity>
              )}
            </View>
            <Image
              style={style.logoDimagine}
              source={require('assets/images/logo_title_vertical.png')}
            />
          </View>
        </View>
      </View>
    );
  }
}

我已经在寻找解决方案,所以我尝试了发现的方法。 我尝试将componentDidMountwillFocuswillBlur一起使用,但是它从未检测到它们:

componentDidMount() {
    const {navigation} = this.props;
    navigation.addListener('willFocus', () =>
      this.setState({focusedScreen: true}),
    );
    navigation.addListener('willBlur', () =>
      this.setState({focusedScreen: false}),
    );
  }

0 个答案:

没有答案