我正在创建一个 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属性