feat: check image size
This commit is contained in:
parent
2f9e639793
commit
04b122bbba
1 changed files with 28 additions and 11 deletions
|
|
@ -1,7 +1,8 @@
|
|||
import { useState } from 'react';
|
||||
import { ScrollView, TouchableOpacity } from 'react-native';
|
||||
import { Alert, ScrollView, TouchableOpacity } from 'react-native';
|
||||
import { Center, Heading, Text, VStack } from '@gluestack-ui/themed';
|
||||
import * as ImagePicker from 'expo-image-picker';
|
||||
import * as FileSystem from 'expo-file-system';
|
||||
|
||||
import { Button } from '@components/Button';
|
||||
import { Input } from '@components/Input';
|
||||
|
|
@ -12,18 +13,34 @@ export function Profile() {
|
|||
const [userPhoto, setUserPhoto] = useState('https://i.pravatar.cc/200');
|
||||
|
||||
async function handleUserPhotoSelection() {
|
||||
const photoSelection = await ImagePicker.launchImageLibraryAsync({
|
||||
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
||||
quality: 1,
|
||||
aspect: [4, 4],
|
||||
allowsEditing: true,
|
||||
});
|
||||
try {
|
||||
const photoSelection = await ImagePicker.launchImageLibraryAsync({
|
||||
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
||||
quality: 1,
|
||||
aspect: [4, 4],
|
||||
allowsEditing: true,
|
||||
});
|
||||
|
||||
if (photoSelection.canceled) {
|
||||
return;
|
||||
if (photoSelection.canceled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const photoURI = photoSelection.assets[0].uri;
|
||||
if (photoURI) {
|
||||
// for some reason type `FileInfo` does not show `size` field
|
||||
const photoInfo = (await FileSystem.getInfoAsync(photoURI)) as {
|
||||
size: number;
|
||||
};
|
||||
|
||||
if (photoInfo.size && photoInfo.size > 5242880) {
|
||||
return Alert.alert('Essa imagem é muito grande. Escolha uma de até 5MB');
|
||||
}
|
||||
|
||||
setUserPhoto(photoURI);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
setUserPhoto(photoSelection.assets[0].uri);
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
|
|||
Loading…
Reference in a new issue