简体   繁体   中英

Cannot read property 'launchImageLibrary' of undefined react-native-image-picker

My feature offers a small image icon as a touchable to upload or take a picture, using "react-native-image-picker": "^3.3.2" .

I'm getting error: Cannot read property 'launchImageLibrary' of undefined , same as this GitHub issue but as you can see, my code already have the import as they told to.

Here's my full code:

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;

Also, In VS Code, I have this error when calling launchImageLibrary : 在此处输入图像描述

I have already perfomed a npx pod-install .

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

It took me a very long time to get this to work but this worked for me.

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

References:

Replacing import ImagePicker from 'react-native-image-picker' with import {launchImageLibrary} from 'react-native-image-picker' works form me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM