【发布时间】:2018-07-30 09:26:39
【问题描述】:
问题:我试图将图像选择器结果转换为 base64 格式,因为它给出了图像 uri 的结果,并且不想像要求那样使用相机插件从图库中选择多张图片。
大多数问题都与此相关问题有关,但对我没有任何帮助,这是我已经解决的问题 Link 1Link 2
这是我尝试将图像转换为 base64 的内容
getImageFromGallery() {
let options = {
maximumImagesCount: 3,
width: 800,
height: 800,
quality: 50,
outputType: 0//image uri
};
this.imagepicker.getPictures(options).then((results) => {
for (var i = 0; i < results.length; i++) {
this.imageurlfrompicker = results[i];
let resizeoptions = {
uri: results[i],
quality: 50,
width: 800,
height: 800
} as ImageResizerOptions;
this.imageResizer
.resize(resizeoptions)
.then((filePath: string) => {
this.imageurlfromresizer = filePath;
this.convertToBase64(filePath, 'image/png').then(
data => {
this.imagebase64 = data.toString(); //base64 of image
console.log(data.toString());
//this.base64Image = "data:image/jpeg;base64," + imageData; // old one
this.imagelist.push(this.imagebase64);
this.imagelist.reverse();
}
);
})
.catch(e => console.log(e));
}
}, (err) => { });
}
转成base64的代码
convertToBase64(url, outputFormat) {
return new Promise((resolve, reject) => {
let img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function () {
let canvas = <HTMLCanvasElement>document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
dataURL;
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
canvas = null;
resolve(dataURL);
};
img.src = url;
});
}
请帮忙解决这个问题
【问题讨论】:
-
为什么不使用 outputType:1 ?
-
嗨@SurajRao,我已经尝试使用输出 1,但它不适用于图像选择器,因为 ionic 官方文档没有提到这个问题
-
表示插件为 outputType 1 提供数据 uri。github.com/Telerik-Verified-Plugins/ImagePicker#options
-
是的,我已经尝试通过设置 outputType 1 但什么也没返回,这就是我发布这个问题的原因
-
我觉得这个插件对你有帮助ionicframework.com/docs/native/base64
标签: ionic-framework ionic2 ionic3 cordova-plugins