如何在Ionic中自动打开相机?

时间:2018-02-11 17:20:38

标签: ionic-framework

嘿,我的相机打开时点击事件“takePhoto”。所以我首先必须在相机打开之前按一个按钮,我实际上可以拍照。但是当我选择相机标签时,我希望相机打开,所以当我打开相机页面时。是否有类似“打开页面时的事件”,所以我的相机会自动打开?

export class PostPage {
  public photos: any;
  public base64Image: string;
  constructor(public navCtrl: NavController, private camera: Camera) {

  }
  ngOnInit(){
    this.photos = [];
  }

  ionViewDidEnter(){
   const options: CameraOptions = {
  quality: 50,
  destinationType: this.camera.DestinationType.DATA_URL,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE
}


this.camera.getPicture(options).then((imageData) => {
 // imageData is either a base64 encoded string or a file URI
 // If it's base64:
 this.base64Image = 'data:image/jpeg;base64,' + imageData;
  this.photos.push(this.base64Image);
  this.photos.reverse();

}, (err) => {
 // Handle error
});
  }
}

1 个答案:

答案 0 :(得分:0)

为此,您可以使用Lifecycle events

例如 ionViewWillEnter 当页面即将进入并成为活动页面时运行。)或 ionViewDidEnter 运行当页面完全进入并且现在是活动页面时。无论是第一次加载还是缓存页面,都会触发此事件。

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

import { Camera, CameraOptions } from '@ionic-native/camera';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public _navCtrl: NavController,
              private _camera: Camera) {
  }

  ionViewDidEnter(){
    this._camera.getPicture(options).then((imageData) => {
    let base64Image = 'data:image/jpeg;base64,' + imageData;
    }, (err) => {
     // Handle error
     });
  }

}