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 { 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 { Center, Heading, Text, VStack } from '@gluestack-ui/themed';
|
||||||
import * as ImagePicker from 'expo-image-picker';
|
import * as ImagePicker from 'expo-image-picker';
|
||||||
|
import * as FileSystem from 'expo-file-system';
|
||||||
|
|
||||||
import { Button } from '@components/Button';
|
import { Button } from '@components/Button';
|
||||||
import { Input } from '@components/Input';
|
import { Input } from '@components/Input';
|
||||||
|
|
@ -12,18 +13,34 @@ export function Profile() {
|
||||||
const [userPhoto, setUserPhoto] = useState('https://i.pravatar.cc/200');
|
const [userPhoto, setUserPhoto] = useState('https://i.pravatar.cc/200');
|
||||||
|
|
||||||
async function handleUserPhotoSelection() {
|
async function handleUserPhotoSelection() {
|
||||||
const photoSelection = await ImagePicker.launchImageLibraryAsync({
|
try {
|
||||||
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
const photoSelection = await ImagePicker.launchImageLibraryAsync({
|
||||||
quality: 1,
|
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
||||||
aspect: [4, 4],
|
quality: 1,
|
||||||
allowsEditing: true,
|
aspect: [4, 4],
|
||||||
});
|
allowsEditing: true,
|
||||||
|
});
|
||||||
|
|
||||||
if (photoSelection.canceled) {
|
if (photoSelection.canceled) {
|
||||||
return;
|
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 (
|
return (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue