开发者问题收集

TypeError:未定义不是一个对象(评估'_reactNativeImagePicker.default.showImagePicker')

2021-01-03
26417

使用 React Image Picker 时我遇到了这个错误: TypeError:undefined 不是对象(评估“_reactNativeImagePicker.default.showImagePicker”)

这是我点击图像选择器功能时发生的情况

移动屏幕截图:

Error Image

这是我的代码:

import React from 'react';
import { View, Text,Button } from 'react-native';
import ImagePicker from 'react-native-image-picker';

const options = {
    title: 'Select Avatar',
    customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
    storageOptions: {
      skipBackup: true,
      path: 'images',
    },
  };

function Picker(){
    const openPicker =()=>{
      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 {
          const source = { uri: response.uri };
      
          // You can also display the image using data:
          // const source = { uri: 'data:image/jpeg;base64,' + response.data };
      
          console.log(source)
        }
      });
    }
    return(
        <View>
            <Button onPress={openPicker} title="Open image picker"></Button>
        </View>
    )
}

export default Picker;
3个回答

我遇到了同样的问题,下面是我解决问题的方法。

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

Casneil Simpson
2021-02-02

如果您的 react-native-image-picker 版本是 3.x.x ,则将上述代码替换为以下几行,

import {launchCamera, launchImageLibrary} from 'react-native-image-picker'; // Migration from 2.x.x to 3.x.x => showImagePicker API is removed.
...

const openPicker =()=>{
  launchCamera(options, (response) => { // Use launchImageLibrary to open image gallery
    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 {
      const source = { uri: response.uri };
  
      // You can also display the image using data:
      // const source = { uri: 'data:image/jpeg;base64,' + response.data };
  
      console.log(source)
    }
  });

阅读 文档

AZITHA INDRAJITH
2021-04-19

问题:
从“react-native-image-picker”导入 ImagePicker

解决方案:
从“react-native-image-picker”导入 * 作为 ImagePicker

chanduthedev
2021-06-09