首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ionic中将base64图像转换为blob

如何在ionic中将base64图像转换为blob
EN

Stack Overflow用户
提问于 2019-01-17 20:56:11
回答 1查看 2.2K关注 0票数 0

我正在与离子3 framework.and工作,我不知道如何转换base64图像到斑点谢谢。

.ts文件

代码语言:javascript
复制
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文件

代码语言:javascript
复制
 <img (click)="openCamera()" [src]="domSanitizer.bypassSecurityTrustUrl(image)" class="before-img" >
EN

回答 1

Stack Overflow用户

发布于 2019-01-17 23:47:21

默认设置是文件URI (blob),如下所示:https://ionicframework.com/docs/native/camera/#CameraOptions

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

代码语言:javascript
复制
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,
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54236452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档