1
0
forked from fr/deguapp

Edited: Pick and take an image

This commit is contained in:
Filip Rojek 2024-05-12 19:12:11 +02:00
parent 630da41536
commit f382150da3

View File

@ -15,14 +15,42 @@ export default function BeerAdd() {
ImagePicker.getCameraPermissionsAsync(); //check if the user has granted permission to access the camera ImagePicker.getCameraPermissionsAsync(); //check if the user has granted permission to access the camera
const pickImage = async () => { const pickImage = async () => {
const permissionResult =
await ImagePicker.requestMediaLibraryPermissionsAsync();
if (permissionResult.granted === false) {
alert("You've refused to allow this appp to access your photos!");
return;
}
// No permissions request is necessary for launching the image library // No permissions request is necessary for launching the image library
let result = await ImagePicker.launchImageLibraryAsync({ const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All, mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true, allowsEditing: true,
aspect: [4, 3], aspect: [3, 4],
quality: 1, // quality: 1,
}); });
// Explore the result
console.log(result);
if (!result.canceled) {
setImage(result.assets[0].uri);
}
};
const openCamera = async () => {
// Ask the user for the permission to access the camera
const permissionResult = await ImagePicker.requestCameraPermissionsAsync();
if (permissionResult.granted === false) {
alert("You've refused to allow this app to access your camera!");
return;
}
const result = await ImagePicker.launchCameraAsync();
// Explore the result
console.log(result); console.log(result);
if (!result.canceled) { if (!result.canceled) {
@ -105,6 +133,9 @@ export default function BeerAdd() {
title="Pick an image from gallery" title="Pick an image from gallery"
onPress={pickImage} onPress={pickImage}
/> />
<Button onPress={openCamera} title="Open camera" />
{image && <Image source={{ uri: image }} style={styles.image} />} {image && <Image source={{ uri: image }} style={styles.image} />}
</View> </View>
<Button title="Add beer" color={colors.green} onPress={addBeer} /> <Button title="Add beer" color={colors.green} onPress={addBeer} />