拍摄照片后,Ionic Cordova相机插件间歇性崩溃(使用Firebase图像上传)

时间:2018-08-24 18:44:56

标签: ios firebase ionic-framework ionic3 google-cloud-firestore

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标志的生产模式运行该应用程序,但并不能解决问题。 我还尝试将质量值降低到一个较低的数字,但这对于后置摄像头仍然无效。

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

2 个答案:

答案 0 :(得分:0)

是的,您是对的。使用带DataURL(即Base64)的相机拍照时。由于图片的大小和质量,我们可能会遇到内存问题。您的质量为'40'很好。对于宽度和高度,您可以设置300px左右。

图像尺寸越大,图像尺寸越大,这将影响内存。

答案 1 :(得分:0)

这不是您查询的合适解决方案,但是在浪费了很多时间之后,我尝试缩小图像并将其保持在2mb以下,并且效果很好。您可以通过指定targetWidth来减小图像尺寸和targetHeight。我将它们都保持在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,
    }
相关问题