首页 文章

如何将图像的URL转换为base64(IONIC 2)

提问于
浏览
0

帮助,如何将图像的URL转换为base64(IONIC 2)

this.camera.getPicture({


sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
  destinationType: this.camera.DestinationType.FILE_URI,
  quality: 100,
  encodingType: this.camera.EncodingType.PNG,
}).then(imageData => {
  console.log('THIS IS THE URI ' + imageData);
  //How do I get the image and convert it to base64 ?
}, error => {
  this.error = JSON.stringify(error);
});

1 回答

  • 0

    你可以这样做:

    this.camera.getPicture({
    sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: this.camera.DestinationType.DATA_URL,
      quality: 100,
      encodingType: this.camera.EncodingType.PNG,
    }).then(imageData => {
      console.log('THIS IS THE Base64' + imageData);
      let base64Image = 'data:image/jpeg;base64,' + imageData;
    }, error => {
      this.error = JSON.stringify(error);
    });
    

    destinationType 支持以下3个选项:

    • DATA_URL :返回base64编码的字符串 . DATA_URL可能非常耗费内存,导致应用程序崩溃或内存不足错误 . 如果可能,请使用FILE_URI或NATIVE_URI

    • FILE_URI :返回文件uri(内容:// media / external / images / media / 2 for Android)

    • NATIVE_URI :返回原生uri(例如,asset-library:// ... for iOS)

相关问题