diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-12-26 12:01:40 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-26 12:01:40 -0600 |
commit | 838fc601c1f89f028862212d169aebe4163c8672 (patch) | |
tree | e00dd942ea0b07f1560e4580c6f273a746c4808c /src/view/com/composer/PhotoCarouselPicker.tsx | |
parent | 8652b74a38f67e7f88890c9c3eb3be090b53462c (diff) | |
download | voidsky-838fc601c1f89f028862212d169aebe4163c8672.tar.zst |
Start with highest quality compression and find a suitable size (#33)
Diffstat (limited to 'src/view/com/composer/PhotoCarouselPicker.tsx')
-rw-r--r-- | src/view/com/composer/PhotoCarouselPicker.tsx | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/view/com/composer/PhotoCarouselPicker.tsx b/src/view/com/composer/PhotoCarouselPicker.tsx index 86d23313d..2afe2e5a5 100644 --- a/src/view/com/composer/PhotoCarouselPicker.tsx +++ b/src/view/com/composer/PhotoCarouselPicker.tsx @@ -7,13 +7,14 @@ import { openCamera, openCropper, } from 'react-native-image-crop-picker' +import {compressIfNeeded} from '../../../lib/images' const IMAGE_PARAMS = { width: 500, height: 500, freeStyleCropEnabled: true, forceJpg: true, // ios only - compressImageQuality: 0.7, + compressImageQuality: 1.0, } export const PhotoCarouselPicker = ({ @@ -25,29 +26,35 @@ export const PhotoCarouselPicker = ({ onSelectPhotos: (v: string[]) => void localPhotos: any }) => { - const handleOpenCamera = useCallback(() => { - openCamera({ - mediaType: 'photo', - cropping: true, - ...IMAGE_PARAMS, - }).then( - item => { - onSelectPhotos([item.path, ...selectedPhotos]) - }, - _err => { - // ignore - }, - ) + const handleOpenCamera = useCallback(async () => { + try { + const cameraRes = await openCamera({ + mediaType: 'photo', + cropping: true, + ...IMAGE_PARAMS, + }) + const uri = await compressIfNeeded(cameraRes, 300000) + onSelectPhotos([uri, ...selectedPhotos]) + } catch (err) { + // ignore + console.log('Error using camera', err) + } }, [selectedPhotos, onSelectPhotos]) const handleSelectPhoto = useCallback( async (uri: string) => { - const img = await openCropper({ - mediaType: 'photo', - path: uri, - ...IMAGE_PARAMS, - }) - onSelectPhotos([img.path, ...selectedPhotos]) + try { + const cropperRes = await openCropper({ + mediaType: 'photo', + path: uri, + ...IMAGE_PARAMS, + }) + const finalUri = await compressIfNeeded(cropperRes, 300000) + onSelectPhotos([finalUri, ...selectedPhotos]) + } catch (err) { + // ignore + console.log('Error selecting photo', err) + } }, [selectedPhotos, onSelectPhotos], ) @@ -60,13 +67,14 @@ export const PhotoCarouselPicker = ({ }).then(async items => { const result = [] - for await (const image of items) { - const img = await openCropper({ + for (const image of items) { + const cropperRes = await openCropper({ mediaType: 'photo', path: image.path, ...IMAGE_PARAMS, }) - result.push(img.path) + const finalUri = await compressIfNeeded(cropperRes, 300000) + result.push(finalUri) } onSelectPhotos([...result, ...selectedPhotos]) }) |