首页 > 解决方案 > 选择相机胶卷在 iOs 的 ionic/cordova 中不起作用没有任何反应

问题描述

当我使用 this.camera.PictureSourceType.PHOTOLIBRARY 调用 getPicture 函数时,什么也没有发生。但是,它在 Android 中确实有效,但在 iO 中不适用,而且 this.camera.PictureSourceType.CAMERA 在 iO 中也有效。

private cameraOptions: CameraOptions = {
  quality: 50,
  destinationType: this.camera.DestinationType.DATA_URL,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE,
};

public async changeProfilePicture() {
  const sheet = await this.actionSheet.create({
    buttons: [{
      text: 'Camera',
      handler: () => {
        this.takePicture();
      },
    },
      {
        text: 'Foto album',
        handler: () => {
          this.choosePicture();
        },
      }
      , {
        text: 'Annuleren',
        role: 'cancel',
        handler: () => {
          sheet.dismiss();
        },
      }],
  });

  sheet.present();
}

private async takePicture() {
  try {
    const image = await this.camera.getPicture({
      ...this.cameraOptions,
      sourceType: this.camera.PictureSourceType.CAMERA,
    });

    this.userService.editProfilePicture(`data:image/jpeg;base64,${image}`).subscribe(user => {
      this.user = user;
    });
  } catch (err) {
    this.handleImageError(err);
  }
}

private async choosePicture() {
  try {

    const image = await this.camera.getPicture({
      ...this.cameraOptions,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
    });

    this.userService.editProfilePicture(`data:image/jpeg;base64,${image}`).subscribe(user => {
      this.user = user;
    });
  } catch (err) {
    this.handleImageError(err);
  }
}

标签: ionic-frameworkcordova-plugins

解决方案


iOS 需要在访问某些本机功能时配置权限。在您的情况下,您需要在应用程序的文件中设置NSPhotoLibraryUsageDescription密钥。Info.plist您可以在 xcode 项目中手动编辑此文件,但最好将其放在config.xml您的 ionic 项目中:

<config-file parent="NSPhotoLibraryUsageDescription" platform="ios" target="*-Info.plist">
 <string>The app needs access to your library to...</string>
</config-file>

推荐阅读