我的功能提供了一个小图像图标,作为可触摸的图标,使用"react-native-image-picker": "^3.3.2"上传或拍照。
我得到的是error: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。
发布于 2021-08-16 14:54:09
我花了很长时间才让它工作,但这对我很有效。
< Button onPress = {
() =>
ImagePicker.launchImageLibrary({
mediaType: 'photo',
includeBase64: false,
maxHeight: 200,
maxWidth: 200,
},
(response) => {
console.log(response);
this.setState({
resourcePath: response
});
},
)
}
title = "Select Image" / >
参考文献:
发布于 2021-04-27 15:04:41
import Library as import * as ImagePicker from 'react-native-image-picker';https://stackoverflow.com/questions/67214916
复制相似问题