世博会图像选择器无法一键请求权限并启动相机

Expo image picker can't request permissions and launch camera in a single click

提问人:Alex 提问时间:5/12/2023 更新时间:5/12/2023 访问量:249

问:

我在 ios 上使用带有 react native 应用程序的 expo 图像选择器来拍摄图像。当用户首次启动应用程序并单击我的“拍照”按钮时,它会提示他们获得使用相机的权限。我希望我的函数在他们授予权限后启动相机,但他们必须再次单击“拍摄图像”才能启动相机。有没有办法在不需要用户第二次点击的情况下完成此操作?如果授予权限,我尝试再次调用 takeImage 函数,但这会导致无限循环,其中 permissions.status 未确定,但授予了 requestPermission。

import * as ImagePicker from 'expo-image-picker';
const [permissions, requestPermission] = ImagePicker.useCameraPermissions(); 
const takeImage = async () => {
    if (permissions.status == 'granted') {
      let data = await ImagePicker.launchCameraAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.Images,
      });
      if (!data.canceled) {
        // Do something with the image
      }
    } else {
      requestPermission().then((res) => {
        // console.log(res['granted']);
        if (res['granted']) {
          console.log('permission granted, launching camera again');
          // Calling takeImage() here throws us into an infinite loop where permissions.status is undetermined, but requestPermission() is granted
          // takeImage();
        }
      });
    }
  };
react-native 权限 ios-camera expo-image-picker

评论


答: 暂无答案