簡體   English   中英

無法讀取未定義的 react-native-image-picker 的屬性“launchImageLibrary”

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

我的功能提供了一個小圖像圖標作為可觸摸的上傳或拍照,使用"react-native-image-picker": "^3.3.2"

我收到錯誤: Cannot read property 'launchImageLibrary' of undefined ,與此GitHub 問題相同,但如您所見,我的代碼已經按照他們的指示進行了導入。

這是我的完整代碼:

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時出現此錯誤: 在此處輸入圖像描述

我已經執行了 npx npx pod-install

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

我花了很長時間才讓它工作,但這對我有用。

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

參考:

將 import ImagePicker from 'react-native-image-picker' 替換為 import {launchImageLibrary} from 'react-native-image-picker' 對我有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM