无法从Expo中的ImagePicker获取base64数据



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

const responsey = await ImagePicker.launchImageLibraryAsync({base64: true, quality: 1,});

它的工作原理很有魅力,但当我试图通过访问摄像头时

const response = await ImagePicker.launchCameraAsync({base64: true, quality: 1,});

承诺永远不会解决,应用程序一直在等待它。我阅读了关于ImagePicker.getPendingResultAsync((方法的文档,并尝试使用如下方法:

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

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

此外,我还注意到,当我不请求base64格式时,来自launchCameraAsync的promise解析得很好。我正在Android 10上重新运行应用程序。

我正在努力解决这个问题,已经在int上呆了好几天了,如果能给我任何关于如何解决这个问题的建议或指导,我将不胜感激。

这样排列代码以获得基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) {

}
};

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

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

最新更新