From ba54c24674eb635ff6d0e61c1b9ad242862ebf5e Mon Sep 17 00:00:00 2001 From: analuizaff Date: Wed, 6 Jan 2021 14:36:15 -0300 Subject: [PATCH] last stable version - ImagePicker --- src/app/components/ImageInput.js | 70 +++++++++----------------------- 1 file changed, 19 insertions(+), 51 deletions(-) diff --git a/src/app/components/ImageInput.js b/src/app/components/ImageInput.js index 623dd8f..6512dc4 100644 --- a/src/app/components/ImageInput.js +++ b/src/app/components/ImageInput.js @@ -19,22 +19,34 @@ function ImageInput({ imageUri, onChangeImage }) { requestPermission(); //requestPermissionCamera(); }, []); - const [modalVisible, setModalVisible] = useState(false); - const requestPermission = async () => { - const { granted } = await Permissions.askAsync(Permissions.CAMERA_ROLL); - if (!granted) alert("Você precisa habilitar permissão para acessar a biblioteca."); - }; - const requestPermissionCamera = async () => { + /* const requestPermissionCamera = async () => { const { granted } = await Permissions.askAsync(Permissions.CAMERA); if(granted) { launchCamera(); } else if (!granted) alert("Você precisa habilitar permissão para acessar a câmera."); }; + + const launchCamera = async () => { + setModalVisible(false); + try { + const result = await ImagePicker.launchCameraAsync({ + allowsEditing: false, + }); + if (!result.cancelled) onChangeImage(result.uri) + } catch (error) { + console.log("Erro ao ler imagem", error); + } + };*/ + + const requestPermission = async () => { + const { granted } = await ImagePicker.requestCameraRollPermissionsAsync(); + if (!granted) alert("Você precisa habilitar permissão para acessar a biblioteca."); + }; const handlePress = () => { - if (!imageUri) setModalVisible(true); + if (!imageUri) launchImageLibrary(); else Alert.alert("Deletar", "Deseja deletar esta imagem?", [ { text: "Sim", onPress: () => onChangeImage(null) }, @@ -43,7 +55,6 @@ function ImageInput({ imageUri, onChangeImage }) { }; const launchImageLibrary = async () => { - setModalVisible(false); try { const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.Images, @@ -55,19 +66,6 @@ function ImageInput({ imageUri, onChangeImage }) { } }; - const launchCamera = async () => { - setModalVisible(false); - try { - const result = await ImagePicker.launchCameraAsync({ - allowsEditing: false, - }); - if (!result.cancelled) onChangeImage(result.uri) - } catch (error) { - console.log("Erro ao ler imagem", error); - } - }; - - return ( @@ -82,36 +80,6 @@ function ImageInput({ imageUri, onChangeImage }) { {imageUri && } - - - - { - setModalVisible(!setModalVisible); - }}> - - - - Selecione uma imagem - - Câmera - - - Galeria - - - { setModalVisible(false) }}> - - Cancelar - - - - - - - ); }