反应本机相机在通过导航器打开 2 次时冻结应用程序

react-native-camera freezes app when opened 2 times via navigator

本文关键字:应用程序 冻结 导航 本机 相机      更新时间:2023-09-26

我在包含在简单<Navigator />组件中的视图中使用 https://github.com/lwansbrough/react-native-camera 库。

一切都按预期工作,直到您导航回主页视图并尝试使用 <Camera /> 重新加载视图。控制台或 Xcode 中没有错误消息,这使得很难查明问题。

当我删除整个<Camera />组件时,导航按预期工作,视图重新加载正常。

github https://github.com/lwansbrough/react-native-camera/issues/80 上目前有一个悬而未决的问题,但由于时间至关重要,我想知道是否有其他人找到了解决此问题的方法并可以分享修复程序。

标准渲染方法:

render() {
    return (
        <View style={styles.outer}>
            <Overlay
                modalVisible={this.state.modalVisible}
                />
            <Camera
                ref="cam"
                style={styles.container}
                captureTarget={Camera.constants.CaptureTarget.disk}
                type={this.state.cameraType}>
                <TouchableHighlight style={styles.circlebutton} onPress={this._takePicture}>
                    <Text>Take Picture</Text>
                </TouchableHighlight>
            </Camera>
            <Image
                source={{uri: this.state.imageURI, isStatic:true}}
                style={{width: this.state.imageURI ? 100 : 0, height: this.state.imageURI ? 100 : 0, opacity: this.state.imageURI ? 1 : 0}}
                />
        </View>
    );
}

试试这个:

在 Xcode 上,转到 RCTCamera.xcodeproj(这是 react 本机库之一(

RCTCameraManager.h

添加属性 @property (nonatomic, strong) RCTCamera *camera;

RCTCameraManager.m

- (UIView *)view
{
    return [[RCTCamera alloc] initWithManager:self bridge:self.bridge];
}

替换为:

- (UIView *)view
{
    if(!self.camera){
        self.camera = [[RCTCamera alloc] initWithManager:self bridge:self.bridge];
        return self.camera;
    }
    return self.camera;
}

希望这是有帮助的。