【问题标题】:Problem with an Unhandeled Promise Rejection in React NativeReact Native 中未处理的 Promise Rejection 问题
【发布时间】:2020-09-18 12:45:23
【问题描述】:

我有两个功能:一个是从相机中拍照,另一个是处理该图像, 当我运行代码时,会显示以下警告:

"[未处理的承诺拒绝:TypeError: undefined is not a function (near '...ImageManipulator.manipulate...')]"

这是我的导入

 import * as Permissions from 'expo-permissions'
 import * as ImagePicker from 'expo-image-picker'
 import * as ImageManipulator from 'expo-image-manipulator'

这里是两个函数,我还应该指出代码在我实现 processImage() 函数之前就可以工作

getImageFromCamera = async () => {
        const cameraPermission = await Permissions.askAsync(Permissions.CAMERA);
        const cameraRollPermission = await Permissions.askAsync(Permissions.CAMERA_ROLL);

        if (cameraPermission.status === 'granted' && cameraRollPermission.status === 'granted') {
            let capturedImage = await ImagePicker.launchCameraAsync({
                allowsEditing: true,
                aspect: [4, 3],
            });
            //If the user didn't cancel the picture
            if (!capturedImage.cancelled) {
                console.log(capturedImage);
                this.processImage(capturedImage.uri);
            }
        }

    }

processImage = async (imageUri) => {
    try {

        let processedImage = await ImageManipulator.manipulate(
            imageUri,
            [
                { resize: { width: 400 } }
            ],
            { format: 'png' }
        );
        

    } catch (error) {
        Alert.alert('Failed to process the Image' )
        
    }
    
    console.log(processedImage);
    this.setState({ imageUrl: processedImage.uri });

}

我是 React Native 和 Expo 的初学者,我无法终生识别错误,如果你们需要更多信息,请告诉我

let processedImage = await ImageManipulator.manipulate 中的承诺失败

但我不知道为什么

【问题讨论】:

  • Permissions.askAsyncImagePicker.launchCameraAsyncImageManipulator.manipulate 可能是一个错误 - 使用 try/catch 来处理来自 await 的错误应该做的那样
  • @JaromandaX Ty 快速响应:D,在使用 try/catch 玩了一下之后,错误出现在 ImageManipulator.manipulate 中,让我更新帖子。
  • 同样的错误?或者现在你得到'Failed to process the Image'
  • 我收到“无法处理图像”警报
  • 所以,您现在已经找到了错误所在...我不知道它是什么...也许error 参数会告诉您

标签: javascript react-native expo


【解决方案1】:

请参阅博览会文档here。根据文档,没有这样的方法manipulate。而不是这个,你应该使用manipulateAsync。所以请更新这一行

let processedImage = await ImageManipulator.manipulate

到这里

let processedImage = await ImageManipulator.manipulateAsync

应该不错。

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 2022-11-18
    • 2017-01-25
    • 2019-12-22
    • 1970-01-01
    • 2018-08-06
    • 2017-07-25
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多