Ionic cordova 相机插件在拍照后间歇性崩溃(使用 Firebase 图片上传)


uploadImage(filePath: string, camera: boolean = false) {
try {
let options: CameraOptions;
if (camera) {
options = {
quality: 40,
destinationType: this._camera.DestinationType.DATA_URL,
encodingType: this._camera.EncodingType.JPEG,
mediaType: this._camera.MediaType.PICTURE,
correctOrientation: true
}
} else {
options = {
destinationType: this._camera.DestinationType.DATA_URL,
sourceType: this._camera.PictureSourceType.PHOTOLIBRARY,
encodingType: this._camera.EncodingType.JPEG,
mediaType: this._camera.MediaType.PICTURE
}
}
this._camera.getPicture(options).then((imageData) => {
const photo = `data:image/jpeg;base64,${imageData}`;
const fileRef = this._afs.ref(filePath);
const task = fileRef.putString(photo, 'data_url');
task.snapshotChanges().pipe(
finalize(() => {
// execute other actions
fileRef.getDownloadURL().subscribe(url => {
if (url) {
this.fileUploadUrl.next(url);
}
})
let toast = this.toastCtrl.create({
message: 'Image upload successfully',
position: 'bottom',
duration: 3000
});
toast.present();
})
).subscribe();
})
} catch (e) {
console.error(e);
let toast = this.toastCtrl.create({
message: 'Image upload cancelled',
position: 'bottom',
duration: 3000
});
toast.present();
}

}

拍摄照片(从实际的iOS设备(后,有时会崩溃,有时可以正常工作。如果我使用前置摄像头,它总是可以工作。 但是,如果我使用后置摄像头,在选择图片后,它会闪烁白屏,然后应用程序重新启动。我怀疑这是否与图像大小或分辨率有关。有时,如果我用后置摄像头拍摄非常低分辨率的照片(例如在弱光环境中(,它会上传得很好。我在网上做了一些研究,有些人建议使用带有--prod标志的生产模式运行该应用程序,但它并没有解决它。 我还尝试将质量值降低到较低的数字,但这仍然不适用于后置摄像头。

我很确定插件已正确添加,并且隐私设置也正确,否则它不允许我拍照。

是的,你是对的。使用带有数据URL的相机(即Base64(拍照时。由于图片大小和质量,我们可能会面临内存问题。你有"40"的质量很好。对于宽度和高度,您可以设置大约 300px。

图像大小越大,图像的大小就越大,这将影响内存。

这不是您查询的正确解决方案,但是在浪费了我的夜晚之后,我试图减少图像并试图将其保持在 2mb 以下,并且效果很好。您可以通过提供目标宽度和目标高度来减小图像大小。我把它们都保持在500以下。

const options: CameraOptions = {
quality: 30, // picture quality
destinationType: this.camera.DestinationType.DATA_URL,
sourceType: this.camera.PictureSourceType.CAMERA,
encodingType: this.camera.EncodingType.PNG,
mediaType: this.camera.MediaType.PICTURE,
cameraDirection: this.camera.Direction.FRONT,
allowEdit: true,
correctOrientation    :false,
targetWidth: 400,
targetHeight: 400,
}

最新更新