从 Expo SDK 50 -> 51 升级后,相机捕获停止工作

问题描述 投票:0回答:1

所以我刚刚从 SDK 50 -> 51 升级,这使得我的应用程序在我使用相机访问组件时一直崩溃,现在我修复了它,我当前的“拍照”方法不起作用。你们知道如何解决这个问题吗?

const captureImage = async () => {
        if (camRef) {
            try {
                const imgData = await camRef.current.takePictureAsync({
                    base64: false,
                    imageType: ImageType.png,
                });

                setImage(imgData);
            } catch (err) {
                console.log(err);
            }
        }
    }

我尝试使用其他方法但不起作用

android ios react-native expo native
1个回答
0
投票

这个例子对我有用:

async function takePicture() {
    if (cameraRef.current) {
      cameraRef.current.takePictureAsync({
        base64: false,
        imageType:ImageType.png,
        onPictureSaved: async (picture) => {
          console.log('Picture saved:', picture.uri)
          setImage(picture.uri as string);
          const asset = await MediaLibrary.createAssetAsync(picture.uri);
        }
      });

    }
  }
        <CameraView ref={cameraRef} style={styles.camera} facing={type as CameraType}>
          <View style={styles.buttonContainer}>
            <TouchableOpacity style={styles.button} onPress={toggleCameraType}>
              <Text style={styles.text}>Flip Camera {selectedRatio}</Text>
            </TouchableOpacity>
            <TouchableOpacity style={styles.button} onPress={takePicture}>
              <Text style={styles.text}>Take Picture</Text>
            </TouchableOpacity>
            {image && <Image source={{ uri: image }} style={{
              flex: 1, width: 100, height: 100, alignSelf: 'flex-end',
              alignItems: 'center', justifyContent: 'center', resizeMode: 'contain'
            }} />}
          </View>
        </CameraView>
© www.soinside.com 2019 - 2024. All rights reserved.