微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法读取未定义的 react-native-image-picker 的属性“launchImageLibrary”

如何解决无法读取未定义的 react-native-image-picker 的属性“launchImageLibrary”

我的功能使用 "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 Code 中,调用 launchImageLibrary 时出现此错误

enter image description here

我已经执行了npx pod-install

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。