简体   繁体   中英

React-native iOS app crashes when taking picture

I am using react-native-camera module in my app. Followed instructions to setup Camera using documentation listed here . After the setup, I can see camera in iOS simulator and also on my iPhone. But when I click on CAPTURE button, my app crashes in simulator and I get following error on my iPhone: iPhone 错误

Here is the code :

import React from 'react';
import {  Text, StyleSheet } from 'react-native';
import Camera  from 'react-native-camera';

class NCamera extends React.Component {
    takePicture() {
        alert('Pressed');

        this.camera.capture()
            .then((data) => {
                console.log(data);
            })
            .catch(err => console.log(err));
    }
    render() {
        return (
            <Camera
                ref={(cam) => {
                    this.camera = cam;
                }}
                style={styles.preview}
                aspect={Camera.constants.Aspect.fill}>
                <Text style={styles.capture} onPress={this.takePicture.bind(this)}>[CAPTURE]</Text>
            </Camera>
        );
    }
}

Please note, if I remove capture method (code below), the app does not crash and works fine both in simulator and iPhone. So there is definitely some problem with the code below which I am not able to figure out what.

this.camera.capture()
            .then((data) => {
                console.log(data);
            })
            .catch(err => console.log(err));

Solution

I forgot to add following to your_project/ios/your_project/Info.plist which allows your app to write permission to user's photo library.

<key>NSPhotoLibraryAddUsageDescription</key>
<string>Your message to user when the photo library is accessed for the first time</string> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM