【问题标题】:Unable to get base64 data from ImagePicker in Expo无法从 Expo 中的 ImagePicker 获取 base64 数据
【发布时间】:2021-05-12 19:18:33
【问题描述】:

我目前正在尝试使用 Expo 和 react-native 开发一个简单的应用程序,但遇到了一个我无法克服的问题。 我需要使用相机和图库获取 base64 格式的图片。为此,我决定使用 ImagePicker expo 组件。 当我使用

访问图片表单库时
const responsey = await ImagePicker.launchImageLibraryAsync({base64: true, quality: 1,});

它的作用就像一个魅力,但是当我尝试通过

访问相机源时
const response = await ImagePicker.launchCameraAsync({base64: true, quality: 1,});

promise 永远不会解决,应用程序一直在等待它。 我阅读了有关 ImagePicker.getPendingResultAsync() 方法的文档并尝试使用 is 这样:

  const prom1 =  ImagePicker.launchCameraAsync({base64: true, quality: 1,});
  const prom2 = ImagePicker.getPendingResultAsync();
  response = await any([prom1,prom2]);

但是这样做的结果是一个空数组(我想象从第二个承诺立即返回) 其中 any 是 promise.any 包中的函数。

另外我注意到,当我不请求 base64 格式时,launchCameraAsync 的承诺可以很好地解决。 我正在重新运行 Android 10 上的应用程序。

我正在努力解决这个问题并且被困在 int 上好几天了,我将不胜感激任何关于如何解决这个问题的建议或指导。

【问题讨论】:

    标签: javascript android react-native expo


    【解决方案1】:

    获取图像的文件 uri,如下所示 - file://.....

    获取此 uri 并使用 Expo 中的 ImageManipulator 将文件更改为 base64 格式。

    【讨论】:

    • 试过这个,但不幸的是,当我尝试获取 base64 格式时,它会冻结 ImageManipulator 返回的承诺。
    【解决方案2】:

    这样安排你的代码以获得基数 64 的值。

    const openCamera = async () => {
        // Ask the user for the permission to access the camera
        const permissionResult = await ImagePicker.requestCameraPermissionsAsync();
    
        if (permissionResult.granted === false) {
          alert("You've refused to allow this appp to access your camera!");
          return;
        }
    
        const result = await ImagePicker.launchCameraAsync({
          base64: true,
          quality: 1,
        });
    
        // Explore the result
        console.log(result.base64);
    
        if (!result.cancelled) {
          
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 2020-03-02
      相关资源
      最近更新 更多