首页 > 解决方案 > Ionic - 拍照后再次调用相机?

问题描述

我目前正在开发一个需要至少 4 张照片的应用程序。每次拍照时我都会尝试重新启动相机,这样他们就不必再次单击应用菜单上的拍照按钮。到目前为止,这是我的代码:

startCamera() {
  if(this.pictureList.length >= 4) {
    this.alertController.create({
      title: this.singleton.TITLE_ERROR,
      message: this.singleton.MESSAGE_TOOMANYPHOTOS,
      buttons: [
        {
          text: 'Ok',
        }
      ]
    }).present();
    return;
  }

  let imageData = "";
  this.camera.getPicture(this.options).then((imageData) => {
    this.pictureList.push(imageData);

    let temp = [];
    for (let i of this.pictureList) i && temp.push(i);

    this.pictureList = temp;
    this.slides.refresh();
  }, (err) => {
    console.log(err);
  });
}

但是,摄像头不重启就进摄像头了?有什么理由不这样做吗?我认为这与我试图在承诺中调用函数有关吗?

谢谢

标签: angularionic-frameworkionic3

解决方案


也许通过递归你可以做到,我给你留下一个未经证实的例子,但这在逻辑上可以为你服务:

我基于将其保存在 Base 64 中的应用程序进行了示例,但根据需要将推送应用到列表中。

pictureList: any[];

  ngOnInit(): void {
    this.pictureList = null;
    this.optionsCamera = {
      quality: 50,
      destinationType: this._camera.DestinationType.DATA_URL,
      encodingType: this._camera.EncodingType.JPEG,
      mediaType: this._camera.MediaType.PICTURE,
      saveToPhotoAlbum: true
    };
    this.optionsGallery = {
      quality: 50,
      destinationType: this._camera.DestinationType.DATA_URL,
      sourceType: this._camera.PictureSourceType.PHOTOLIBRARY
    };
  }

  startCamera(): void {
    if (this.pictureList.length <= 4) {
      console.log('Open in length: ' + this.pictureList.length);
      this.getPhoto(this.optionsCamera);
      this.startCamera();
    }
  }

  getPhoto(options): void {
    this._camera.getPicture(options).then(
      imageData => {
        let base64Image = "data:image/jpeg;base64," + imageData;
        this.pictureList.push(base64Image);
      },
      err => {
        console.log("Could not open the camera: " + err);
      }
    );
  }

推荐阅读