相机和照片库一起离子2

时间:2017-07-25 09:45:21

标签: ionic-framework ionic2 ionic-native

我使用原生插件http://ionicframework.com/docs/native/camera/访问相机照片库。 如有必要,我需要一个解决方案来访问相机并切换到相片库。 是否有这样的插件?

1 个答案:

答案 0 :(得分:2)

正如Swapnil Patwa在评论中所说,你可以向用户提出一个ActionSheet:

public showActionSheet() {
    let actionSheet = this.actionSheetCtrl.create({
      buttons: [{
        text: 'Load from gallery',
        handler: () => {this.loadImage(this.camera.PictureSourceType.PHOTOLIBRARY);}
      },{
        text: 'Take a photo',
        handler: () => {this.loadImage(this.camera.PictureSourceType.CAMERA);}
      },{
        text: 'Cancel',
        role: 'cancel'
      }]
    });
    actionSheet.present();
  }

  private loadImage(selectedSourceType:number){
    let cameraOptions: CameraOptions = {
      sourceType: selectedSourceType,
      destinationType: this.camera.DestinationType.DATA_URL,
      quality: 100,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      correctOrientation: true,
    }
    this.camera.getPicture(cameraOptions).then((imageData) => {
      if(imageData!=null){
        // Do with the image data what you want.
      }
    });
  }