I am building an Ionic App and I am getting this error when I try to convert to base64 an image from an URL.
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
My code is the following:
public getBase64Image(imgUrl: string): Promise<string> {
if (!imgUrl.includes("http")) {
return;
}
return new Promise<string>(resolve => {
let img = new Image();
img.src = imgUrl;
img.crossOrigin = "anonymous"
img.onload = (() => {
let canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
let ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
let dataURL = canvas.toDataURL("image/png");
resolve(dataURL);
});
});
}
I've read other questions where setting anonymous
in crossOrigin
was the solution, but I already have it.
Hope you can help me, thanks.
EDIT 1
As a note, I don't get this the first time that I convert an image to base64 but I get it in the following times I try to edit an image.