React-native iOS 应用在拍照时崩溃

React-native iOS app crashes when taking picture

我在我的应用程序中使用 react-native-camera 模块。按照说明使用列出的文档 here 设置相机。设置后,我可以在 iOS 模拟器和我的 iPhone 上看到摄像头。但是当我点击 CAPTURE 按钮时,我的应用程序在模拟器中崩溃并且我的 iPhone 出现以下错误:

这是代码:

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>
        );
    }
}

请注意,如果我删除 capture 方法(下面的代码),应用程序不会崩溃并且在模拟器和 iPhone 中都可以正常工作。所以下面的代码肯定有问题,我无法弄清楚是什么。

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

解决方案

我忘了在 your_project/ios/your_project/Info.plist 中添加以下内容,它允许您的应用程序向用户的照片库写入权限。

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