1

我正在创建一个 react-native 应用程序来使用 expo-camera 模块。

所以我将相机变量声明为

let camera: any = null;

我从返回中传递对此的引用

<Camera style = {{flex: 1, opacity: camOpacity}}
          type = {state.type}
          ratio = "2:1"
          ref = {async (ref) => (camera = ref)}>

但是当我运行该应用程序时,它会正常启动,但是在尝试拍照时会出现错误:

[Unhandled promise rejection: TypeError: null is not an object (evaluating 'camera.pictureSize')]

此错误发生在:

console.log(camera.pictureSize);

但它也发生在:

console.log(" --> Taking image");
    const opts = {
       skipProcessing: true,
       exif: false,
       quality: 0
    };
    let photo = await camera.takePictureAsync(opts);

当我注释掉 console.log(camera.pictureSize)时,来自camera.takePictureAsync (opts)部分;

由于某种原因,参考可能没有被检测到。

我的 package.json 是:

"dependencies": {
    "expo": "~39.0.2",
    "expo-2d-context": "0.0.2",
    "expo-asset": "~8.2.0",
    "expo-camera": "~9.0.0",
    "expo-gl": "~9.1.1",
    "expo-image-manipulator": "~8.3.0",
    "expo-permissions": "~9.3.0",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-39.0.3.tar.gz",
    "react-native-web": "~0.13.12",
    "react-native-screens": "~2.10.1",
    "react-native-svg": "~12.1.0",
    "mem": "^8.0.0",
    "@unimodules/core": "~5.5.0"
  }

我读到在当前的 expo-camera 版本中有一些这样的错误,但即使我降级了包和依赖项,它仍然是持久的。

任何帮助将非常感激。

编辑:

const photo = async (): Promise<CameraCapturedPicture> | undefined  => {
      if(camera){
        console.log(" --> Taking image");
        const opts = {
          skipProcessing: true,
          exif: false,
          quality: 0
        };
        return await camera.takePictureAsync(opts);
      }
    }
    
    console.log(" --> Resizing image");
    const {uri} = await ImageManipulator.manipulateAsync(
      photo.uri,
      [
        { resize: {width: 256, height: 512}},
        { crop: {originX: 0, originY: 128, width: 256, height: 256}}
      ]
    );

我根据 Linda 的建议更改了代码,但现在的错误是 Promise 不是有效类型,并且photo.uri没有uri属性

4

1 回答 1

1

camera您将ref 对象初始化为null. 所以在你调用它的任何函数之前,你需要验证它实际上已经被设置为 aCamera并且 is not still null。我怀疑这是您的拍照功能出错的原因。请注意,他们在调用相机上的方法之前会检查文档。if (this.camera) {

console.log(camera.pictureSize)即使相机不是null因为该属性不存在,也是错误的。 pictureSize是您传递给Camera组件的道具。它不是相机实例上存在的属性。您应该查看文档

而不是使用any,您可以将变量声明更改为Camera | null,这应该可以帮助您查看可用的属性和方法。

let camera: Camera | null = null;

您可以检查它camera不在null您调用拍照的方法中:

const takePicture = async (): Promise<CameraCapturedPicture> | undefined => {
  if (camera) {
    console.log(" --> Taking image");
    const opts = {
      skipProcessing: true,
      exif: false,
      quality: 0
    };
    return await camera.takePictureAsync(opts);
  }
};

或者您可以在调用方法之前检查并将camera变量传递给方法,以便打字稿知道它不是null.

const executeTakePicture = async (cam: Camera): Promise<CameraCapturedPicture> => {
  console.log(" --> Taking image");
  const opts = {
    skipProcessing: true,
    exif: false,
    quality: 0
  };
  return await cam.takePictureAsync(opts);
};

const maybeTakePicture = async (): Promise<CameraCapturedPicture> | undefined => {
  if (camera) {
    return await executeTakePicture(camera);
  } else {
    //...
  }
}
于 2020-10-11T19:54:30.083 回答