【问题标题】:How to convert base64 image to blob in ionic如何将base64图像转换为离子中的blob
【发布时间】:2019-01-17 23:47:21
【问题描述】:

我正在使用 ionic 3 框架。我不知道如何将 base64 图像转换为 blob 谢谢。

.ts 文件

openCamera() {
    let actionSheet = this.actionSheetCtrl.create({
      title: 'Edit Your Profile Picture',
      buttons: [
        {
          text:'Camera',
          icon: 'ios-camera',
          role: 'destructive',
          handler: () => {
            const options: CameraOptions = {
              quality: 100,
              destinationType: this.camera.DestinationType.DATA_URL,
              saveToPhotoAlbum: true,
              mediaType: this.camera.MediaType.PICTURE
            }

            this.camera.getPicture(options).then((imageData) => {
              this.image = 'data:image/jpeg;base64,' + imageData;

            }, (err) => {
              alert(err)
            });
          }
        },
        {
          text: 'Gallery',
          icon: 'ios-images',
          handler: () => {
            const options: CameraOptions = {
              quality: 50,
              destinationType: this.camera.DestinationType.DATA_URL,
              encodingType: this.camera.EncodingType.JPEG,
              mediaType: this.camera.MediaType.PICTURE,
              correctOrientation: true,
              sourceType:this.camera.PictureSourceType.PHOTOLIBRARY,
            }

            this.camera.getPicture(options).then((imageData) => {
              // imageData is either a base64 encoded string or a file URI
              // If it's base64 (DATA_URL):

              this.image = 'data:image/jpeg;base64,' + imageData;
              //this.image = base64Image;
             // alert(base64Image);
            }, (err) => {
              // Handle error
              console.log(err);
            });
          }
        },
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {

          }
        }
      ]
    });

    actionSheet.present();
  }

.html 文件

 <img (click)="openCamera()" [src]="domSanitizer.bypassSecurityTrustUrl(image)" class="before-img" >

【问题讨论】:

  • 请说明您遇到了什么问题,您是否遇到错误等?
  • 是 :- data:image/jpeg;base64,file:///storage/emulated/0/Android/data/com.toon.com/cache/IMG-20190117-WA0006.jpg? 1547785733893:1 GET data:image/jpeg;base64,file:///storage/emulated/0/Android/data/com.toon.com/cache/IMG-20190117-WA0006.jpg?1547785733893 net::ERR_INVALID_URL跨度>

标签: ionic-framework


【解决方案1】:

默认设置是文件 URI (blob),根据: https://ionicframework.com/docs/native/camera/#CameraOptions

因此,您可以在此处更改当前选项以请求 FILE URI 而不是 DATA URL(因为您当前的选项设置为使用 base64 的 DATA URL):

const options: CameraOptions = {
              quality: 50,
              destinationType: this.camera.DestinationType.FILE_URI,
              encodingType: this.camera.EncodingType.JPEG,
              mediaType: this.camera.MediaType.PICTURE,
              correctOrientation: true,
              sourceType:this.camera.PictureSourceType.PHOTOLIBRARY,
}

您也可以省略显式设置,然后理论上将使用默认值...

【讨论】:

  • 出现错误:data:image/jpeg;base64,file:///storage/emulated/0/Android/data/com.toon.com/cache/IMG-20190117-WA0006.jpg? 1547785733893:1 GET data:image/jpeg;base64,file:///storage/emulated/0/Android/data/com.toon.com/cache/IMG-20190117-WA0006.jpg?1547785733893 net::ERR_INVALID_URL跨度>
猜你喜欢
  • 2019-11-18
  • 2022-08-12
  • 2021-09-22
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多