undefined不是对象(评估ImagePickerManager.showImagePicker)

时间:2017-12-15 10:11:59

标签: react-native

var ImagePicker = require('react-native-image-picker');
call() {   
    var options = {
      title: 'Select Avatar',
      customButtons: [
        {name: 'fb', title: 'Choose Photo from Facebook'},
      ],
      storageOptions: {
        skipBackup: true,
        path: 'images'
      }
    };
      ImagePicker.showImagePicker(options, (response) => {
      console.log('Response = ', response);

      if (response.didCancel) {
        console.log('User cancelled image picker');
      }
      else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      }
      else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
      }
      else {
        let source = { uri: response.uri };

        // You can also display the image using data:
        // let source = { uri: 'data:image/jpeg;base64,' + response.data };

        this.setState({
          avatarSource: source
        });
      }
    });
  }

我从render onClick调用此函数。我正在使用

  

react-native-image-picker@0.26.7

但是它给了我undefined不是一个对象错误。另外请告诉我如何将其链接到电话库以选择图像。请建议我解决方案 enter image description here

3 个答案:

答案 0 :(得分:2)

如果您更新导入:

import * as ImagePicker from 'react-native-image-picker';

ImagePicker.showImagePicker(options, (response) => {
         // code here
};

一切都会起作用

答案 1 :(得分:1)

首先,您必须链接模块。

react-native link react-native-image-picker

然后检查以下文件:

机器人/应用/的build.gradle:compile project(':react-native-image-picker')

机器人/ setting.gradle:

include ':react-native-image-picker'
project(':react-native-image-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-image-picker/android')

机器人/应用/ SRC /..../ MainApplication enter image description here

问题在于:

https://github.com/react-community/react-native-image-picker/issues/414#issuecomment-265060406

答案 2 :(得分:0)

我认为这是解决这个问题的好方法:

import * as ImagePicker from "react-native-image-picker"

<TouchableOpacity style={styles.CameraIconContent}
          onPress={() =>
            ImagePicker.launchImageLibrary(
              {
                mediaType: 'photo',
                includeBase64: false,
                maxHeight: 200,
                maxWidth: 200,
              },
              (response) => {
                console.log(response);
                this.setState({resourcePath: response});
              },
            )
          }
                    >
                    <Image
                        source={CAMERA_ICON}
                            resizeMode = "contain"
                            style={{
                                width: 20,
                                height: 20
                            }}
                    />
            </TouchableOpacity>