我的功能提供了一个小图像图标作为一个可触摸的上传或拍照,使用"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
。
4条答案
按热度按时间p3rjfoxz1#
我花了很长时间才让它起作用,但它对我起作用了。
参考文献:
xriantvc2#
我使用了“react-native-image-picker”:“^4.8.5”,
我只是重新启动metro“npxReact Native启动”或“npxReact Native启动--重置缓存”
并重新运行地铁捆绑“npxReact原生运行安卓”
我的密码是
to94eoyn3#
谢谢Akash Suryawanshi,这个工作
import * as ImagePicker from 'react-native-image-picker';
9rnv2umw4#