BASE64插件不适用于离子3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BASE64插件不适用于离子3相关的知识,希望对你有一定的参考价值。
我需要将图像转换为base 64格式,我从相机或图库中捕获,然后将该字符串发送到我的REST Api.I尝试了很多东西,但我遇到的最稳定的解决方案是使用Base 64 package
-https://ionicframework.com/docs/native/base64/但是它既没有给出我有任何错误,也没有生成字符串。以下是我的代码:
this.camera.getPicture(options).then((imagePath) => {
this.base64.encodeFile(imagePath).then((base64Image: string) => {
console.log(base64Image);
}, (err) => {
console.log(err);
this.base64Image=err;
});
if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
});
}
else {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}, (err) => {
this.presentToast('Error while selecting image.');
});
答案
你不需要一个插件来进行编码,相机插件返回一个带有正确选项的BASE64字符串,这是我工作代码的一部分。
let opcoes = {
maximumImagesCount: 1,
sourceType: 1,
encodingType: this.camera.EncodingType.JPEG,
destinationType: 0, // USE THIS TO RETURN BASE64 STRING
correctOrientation: true
};
this.camera.getPicture(opcoes).then(res => {
this.picture= res;
console.log('check the string', res);
// IT RETURNS ONLY THE IMAGE STRING WITHOUT THE data:image, IF YOU WANT TO
// SHOW YOUR PICTURE JUST APPEND THE STRING BELLOW TO YOUR RETURNED STRING AND USE IT TO DISPLAY
this.pictureForExibition = 'data:image/jpeg;base64,' + res;
}, err => {
console.log(err);
});
在您的html中
<!-- JUST USE AN *ngIf TO CHECK IF THE PROPERTY ISN'T NULL OR IT'LL THROW ERROR FOR THE INVALID SRC -->
<img [src]="pictureForExibition" *ngIf="pictureForExibition" alt="base64 image" />
希望这可以帮助
以上是关于BASE64插件不适用于离子3的主要内容,如果未能解决你的问题,请参考以下文章
离子电容器:如何将 ImagePicker 的类型更改为 Base64?