ios - React Native Undefined 不是对象(评估 'this.camera.capture' )

标签 ios reactjs react-native

export class CameraView extends Component {
render(){
    return(
        <View style={{
                flex: 1,
                width: '100%',
                height: '100%',
            }}>
            <Camera
                ref={(cam) => {
                    this.camera = cam;
                }}
                style={styles.preview}
                aspect={Camera.constants.Aspect.fill}>
                <CaptureControls></CaptureControls>
                <Image></Image>
            </Camera>
        </View>
    );
  }
}


export class CaptureControls extends Component {
render(){
    return(
        <View style={{
                flexDirection: 'row',
                alignItems: 'center',
                justifyContent: 'center',
                bottom: 30,
            }}>
            <Image style={{
                    width: 30,
                    height: 30,
                    marginRight: 75,
                }} source={require('img/opengallery.png')}></Image>
            <TouchableHighlight onPress={this.takePicture.bind(this)}>
                <Image style={{
                        width: 70,
                        height: 70,
                    }} source={require('img/takepicture.png')}> </Image>
            </TouchableHighlight>
            <Image style={{
                        width: 30,
                        height: 30,
                        marginLeft: 75,
                }} source={require('img/switchcam.png')}></Image>
        </View>
    );
}
takePicture() {
    this.camera.capture()
    .then((data) => console.log(data))
    .catch(err => console.error(err));
}
}

这是我的代码。当按下拍照按钮时,我得到 undefined is not an object。我的代码有什么问题? takePicture() 虽然在 Text 元素上工作。我在图像周围添加了一个 TouchableHighlight,并将 onPress 添加到 TouchableHighlight,但现在它崩溃了。

亲切的问候, 马修

最佳答案

我猜你同时想到了这一点。 “相机”变量在 CameraView 的上下文中分配,但代码尝试从 CaptureControls 中访问它。

关于ios - React Native Undefined 不是对象(评估 'this.camera.capture' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42873000/

相关文章:

ios - YTPlayerView 视频做了 Ended iOS

javascript - 上下文映射函数中的 jsx-no-bind

javascript - 每次在 React 中创建元素时,我找不到在元素上获取 css 过渡的方法

android - 是否可以在 react-native 中创建基于客户端工作空间的应用程序?

reactjs - React Native - 退出应用程序时调用方法

ios - 将 StoryBoard 中的 IBAction 链接到 ".h"文件或 ".m"文件之间有什么区别吗?

ios - MGLPolyLine 颜色更改不起作用 - Swift 4、Mapbox

javascript - React(Redux + Redux Saga)和 MVC

javascript - 构建高阶组件并将其应用于 redux connect

ios - 类型 'NSAttributedStringKey'(又名 'NSString')没有成员 'foregroundColor'