Oli*_*via 7 react-native react-native-camera expo react-native-image-picker
我尝试了很多尝试来启动 react-native-image-picker 并使用我的 RN 应用程序。我正在使用 Expo 和 VS Code,并且没有使用 Xcode 或 Android Studio 运行该应用程序。在应用程序中获取相机胶卷的方法似乎有很多,我不确定哪条路是最好的。似乎没有人为我工作,所以我想选择最佳路径并专注于使那条路径有效。
我正在关注文档:https : //github.com/react-native-community/react-native-image-picker
我尝试过的事情:
我的代码:
import React, {useState, useEffect} from 'react';
import {StyleSheet, View, TextInput, TouchableOpacity, Text, CameraRoll } from 'react-native'
import ImagePicker from 'react-native-image-picker';
// import * as ImagePicker from 'expo-image-picker';
import Constants from 'expo-constants';
const PicturesScreen = ({navigation}) => {
const [pictures, setPictures] = useState([]);
getPermissionAsync = async () => {
if (Constants.platform.ios) {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
alert('Sorry, we need camera roll permissions to make this work!');
}
}
};
useEffect(() => {
getPermissionAsync();
}, []);
selectPhotoTapped = () => {
const options = {
quality: 1.0,
maxWidth: 500,
maxHeight: 500,
storageOptions: {
skipBackup: true,
},
};
ImagePicker.showImagePicker(options, response => {
if (response.didCancel) {
console.log('User cancelled photo picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
let source = {uri: response.uri};
console.log('source: ' + source);
// You can also display the image using data:
// let source = { uri: 'data:image/jpeg;base64,' + response.data };
setPictures({
picture: source
});
}
});
};
return (
<View style = {styles.container}>
<TouchableOpacity style = {styles.buttonContainerPhoto} onPress={()=> selectPhotoTapped()}>
<Text style={styles.buttonText} >
Upload Photos
</Text>
</TouchableOpacity>
<TouchableOpacity style = {styles.buttonContainer} onPress={()=> navigation.navigate('NextScreen')}>
<Text style={styles.buttonText} >
Next
</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
...
}
});
export default PicturesScreen;
Run Code Online (Sandbox Code Playgroud)
我确保链接了这些包,我也卸载并重新安装并从头开始几次。我已将版本降级以使其正常工作,但我仍然继续收到以下错误消息之一:
react-native-image-picker: NativeModule.ImagePickerManager 为空
或者
无法读取未定义的属性“showImagePicker”。
或者
undefined 不是一个对象(评估 'imagepickerManager.showimagepicker')
是否因为我使用 Expo 而导致问题?我应该只使用带有 react-native 的 CameraRoll 吗?
zay*_*rix 10
如果您使用 Expo,请使用expo-image-picker。
任何需要使用的东西react-native link都不适用于 Expo,除非声明它已经包含在 Expo 中。
| 归档时间: |
|
| 查看次数: |
8701 次 |
| 最近记录: |