mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 02:10:18 +01:00
59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
import { useState, } from 'react';
|
|
import { Alert } from 'react-native';
|
|
import { launchImageLibraryAsync, requestMediaLibraryPermissionsAsync } from 'expo-image-picker';
|
|
|
|
export function usePickImage() {
|
|
const [image, setImage] = useState(null);
|
|
|
|
const pickImage = async () => {
|
|
try {
|
|
const permissionResult = await requestMediaLibraryPermissionsAsync();
|
|
|
|
if (permissionResult.granted === false) {
|
|
Alert.alert("Permission refusée", "Activez l'accès au stockage ou à la gallerie dans les paramètres.");
|
|
return;
|
|
}
|
|
|
|
let result = await launchImageLibraryAsync({
|
|
mediaTypes: ['images'],
|
|
allowsMultipleSelection: false,
|
|
allowsEditing: true,
|
|
aspect: [4, 3],
|
|
quality: 1,
|
|
});
|
|
|
|
if (!result.canceled && result) {
|
|
setImage(result.assets[0]);
|
|
}
|
|
else {
|
|
console.log('Image picker cancelled.');
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error('Error picking image;', error);
|
|
Alert.alert('Erreur', "Une erreur est survenue lors de la sélection d'une image.");
|
|
}
|
|
}
|
|
|
|
function sendImage(location) {
|
|
if (image) {
|
|
let data = new FormData();
|
|
data.append('file', {
|
|
uri: image.uri,
|
|
name: 'photo.jpg',
|
|
type: 'image/jpeg',
|
|
});
|
|
|
|
fetch(location , {
|
|
method: 'POST',
|
|
body: data,
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data',
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
return {image, pickImage, sendImage};
|
|
}
|