import CameraRoll from '@react-native-community/cameraroll'; import {RefObject} from 'react'; import {Alert} from 'react-native'; import { RNCamera, TakePictureOptions, TakePictureResponse, } from 'react-native-camera'; import {ProcessingManager} from 'react-native-video-processing'; import ImagePicker, {Image, Video} from 'react-native-image-crop-picker'; import {ERROR_UPLOAD} from '../constants/strings'; /* * Captures a photo and pauses to show the preview of the picture taken */ export const takePicture = ( cameraRef: RefObject, callback: (pic: TakePictureResponse) => void, ) => { if (cameraRef !== null) { cameraRef.current?.pausePreview(); const options: TakePictureOptions = { forceUpOrientation: true, orientation: 'portrait', writeExif: false, }; cameraRef.current?.takePictureAsync(options).then((pic) => { callback(pic); }); } }; export const saveImageToGallery = (capturedImageURI: string) => { CameraRoll.save(capturedImageURI, {album: 'Recents', type: 'photo'}) .then((_res) => Alert.alert('Saved to device!')) .catch((_err) => Alert.alert('Failed to save to device!')); }; export const navigateToImagePicker = (callback: (pic: Image) => void) => { ImagePicker.openPicker({ smartAlbums: [ 'Favorites', 'RecentlyAdded', 'SelfPortraits', 'Screenshots', 'UserLibrary', ], mediaType: 'photo', }) .then((pic) => { console.log(ProcessingManager.compress); console.log('here'); ProcessingManager.compress(pic, options.compress) // like VideoPlayer compress options .then((data: any) => console.log(data)); console.log('over'); callback(pic); }) .catch((err) => { if (err.code && err.code !== 'E_PICKER_CANCELLED') { Alert.alert(ERROR_UPLOAD); } }); }; export const navigateToVideoPicker = (callback: (vid: Video) => void) => { ImagePicker.openPicker({ mediaType: 'video', }) .then(async (vid) => { if (vid.path) { ProcessingManager.compress(vid, options.compress) // like VideoPlayer compress options .then((data: any) => console.log(data)); callback(vid); } }) .catch((err) => { if (err.code && err.code !== 'E_PICKER_CANCELLED') { Alert.alert(ERROR_UPLOAD); } }); }; const options = { compress: { width: 720, height: 1280, bitrateMultiplier: 3, saveToCameraRoll: true, // default is false, iOS only saveWithCurrentDate: true, // default is false, iOS only minimumBitrate: 300000, // removeAudio: true, // default is false }, }; export const showGIFFailureAlert = (onSuccess: () => void) => Alert.alert( 'Warning', 'The app currently cannot handle GIFs, and will only save a static image.', [ { text: 'Cancel', onPress: () => {}, style: 'cancel', }, { text: 'Post', onPress: onSuccess, style: 'default', }, ], { cancelable: true, onDismiss: () => Alert.alert( 'This alert was dismissed by tapping outside of the alert dialog.', ), }, );