expo install & expo start --ios
const mockDLUrl = () => {
const size = Math.ceil(Math.random() * 6) * 120;
return `https://fillmurray.com/${size}/${size}`;
};
...
const cameraRef = React.useRef<Camera | null>(null);
const isEmulator = !DeviceInfo.isDevice;
const { style, onCameraReady, type } = props;
// if it's running on Simulator, immediately return that camera is ready
useEffect(() => {
if (isEmulator) {
onCameraReady();
}
}, [isEmulator, onCameraReady]);
const takePictureAsync = async (
options: CameraPictureOptions
): Promise<PictureResult> => {
if (cameraRef.current instanceof Camera) {
return cameraRef?.current?.takePictureAsync(options);
}
// When running on Simulator, get random file from the internet
const downloadPath = `${
FileSystem.documentDirectory
}/${randomString()}.jpg`;
await FileSystem.downloadAsync(mockDLUrl(), downloadPath);
return {
uri: downloadPath,
};
};
const pausePreview = () => {
if (cameraRef.current instanceof Camera) {
cameraRef.current.pausePreview();
}
};
const resumePreview = () => {
if (cameraRef.current instanceof Camera) {
cameraRef.current.resumePreview();
}
};
// Expose the reference to Parent Component
useImperativeHandle(ref, () => ({
pausePreview,
resumePreview,
takePictureAsync,
}));
return (
<>
{isEmulator ? (
<View style={style} />
) : (
<Camera
ref={cameraRef}
onCameraReady={onCameraReady}
style={style}
type={cameraType}
/>
)}
</>
);