无法读取未定义的React Native图像选取器的属性“launchImageLibrary”

voase2hg  于 2023-03-19  发布在  React
关注(0)|答案(4)|浏览(218)

我的功能提供了一个小图像图标作为一个可触摸的上传或拍照,使用"react-native-image-picker": "^3.3.2"
我收到错误:Cannot read property 'launchImageLibrary' of undefined,与此GitHub issue相同,但如您所见,我的代码已经按照要求导入。
下面是我的完整代码:

import React from 'react';
import {
    StyleSheet,
    Image,
    TouchableOpacity,
    Alert
} from 'react-native';
import { launchImageLibrary } from 'react-native-image-picker';

const ImageUpload: React.FC<any> = ({}) => {

    function showMessage() {
        Alert.alert("Upload image", "Choose a option", [
            {
                text: 'Camera',
                onPress: () => openCamera(),
            },
            {
                text: 'Gallery',
                onPress: () => openLibrary()
            },
        ]);
    }

    const openLibrary = () => {

        const options = {
            storageOptions: {
              skipBackup: true,
              path: 'images',
            },
          };
        launchImageLibrary(options, (response) => {
            console.log(response);
        });

    }

    const openCamera = () => {
        //ongoing
    }

    return(
        <>
            <TouchableOpacity onPress={()=>showMessage()}>
                <Image source={require('./picture.png')} style={{ width: 70, height: 70 }}/>
            </TouchableOpacity>
        </>
     );
};

const style = StyleSheet.create({
    ImageIcon: {
        justifyContent: "center",
        alignItems: "center",
    }

});

export default ImageUpload;

另外,在VS代码中,我在调用launchImageLibrary时遇到了这个错误:

我已经执行了npx pod-install

p3rjfoxz

p3rjfoxz1#

我花了很长时间才让它起作用,但它对我起作用了。

< Button onPress = {
  () =>
  ImagePicker.launchImageLibrary({
      mediaType: 'photo',
      includeBase64: false,
      maxHeight: 200,
      maxWidth: 200,
    },
    (response) => {
      console.log(response);
      this.setState({
        resourcePath: response
      });
    },
  )
}
title = "Select Image" / >

参考文献:

xriantvc

xriantvc2#

我使用了“react-native-image-picker”:“^4.8.5”,
我只是重新启动metro“npxReact Native启动”或“npxReact Native启动--重置缓存”
并重新运行地铁捆绑“npxReact原生运行安卓”
我的密码是

import { launchImageLibrary } from "react-native-image-picker";

 handleChangePhoto = () => {
    launchImageLibrary({ noData: true }, (response) => {
      console.log(response);
      // if (response) {
      //   setPhoto(response);
      // }
    });
  };
to94eoyn

to94eoyn3#

谢谢Akash Suryawanshi,这个工作import * as ImagePicker from 'react-native-image-picker';

9rnv2umw

9rnv2umw4#

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

相关问题