【问题标题】:Ionic cordova camera plugin crashes intermittently after a picture is taken (with Firebase image upload)拍摄照片后,Ionic cordova 相机插件间歇性崩溃(使用 Firebase 图像上传)
【发布时间】:2018-08-24 18:44:55
【问题描述】:
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 标志以生产模式运行应用程序,但这并没有解决它。 我还尝试将质量值降低到一个较低的数字,但这仍然不适用于后置摄像头。

我很确定插件添加正确,隐私设置也正确,否则我将无法拍照。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我也尝试过只使用 FILE_URI 而不是 DATA_URL,但是从后置摄像头拍照时它仍然崩溃。
    • 不,很遗憾。
    • 那应该是你手机内存的问题。尝试在另一部具有良好内存的手机中验证
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多