我目前正在开发一个小型应用程序,我想将相机胶卷中的图像加载到应用程序中。我已经在 Expo Image Picker 文档的帮助下构建了一个组件来执行此操作。遗憾的是,我总是在我的博览会客户端中收到以下警告,并且我无法从相机胶卷中选取任何图像。当我单击按钮选择它们时,它就保持原样。
我的代码:
import React, {useEffect, useState} from 'react';
import { View, Image, TouchableOpacity, PermissionsAndroid, Alert, Platform, StyleSheet } from 'react-native';
import * as Permissions from 'expo-permissions';
import ImagePicker from 'expo-image-picker';
import { MaterialIcons } from '@expo/vector-icons'
export default function ImageChooser() {
const [imageSource, setImageSource] = useState([]);
getPermissionAsync = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
alert('Sorry, we need camera roll permissions to make this work!');
}
};
useEffect(() => {
getPermissionAsync();
}, []);
_getPhotoLibrary = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3]
});
if (!result.cancelled) {
setImageSource({ image: result.uri });
}
}
return (
<View style={styles.container}>
<TouchableOpacity style={styles.button} onPress={() => _getPhotoLibrary()}>
{!imageSource && <MaterialIcons name="add-a-photo" style={styles.icon} size={36} />}
{/* {imageSource && <Image source={{uri:imageSource}} style={styles.image} />} */}
</TouchableOpacity>
</View>
)
}
const styles = StyleSheet.create({
container: {
paddingLeft: 20,
paddingVertical: 10
},
button: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
width: 200,
height: 150,
borderWidth: 1,
borderColor: '#C6C6C8',
borderRadius: 5,
backgroundColor: '#fff'
},
image: {
width: 200,
height: 150
},
icon: {
color: '#C6C6C8'
}
})
冉冉说
杨魅力
largeQ
慕无忌1623718
相关分类