const base64Str: string = await new Promise(resolve => { const img = new Image() img.crossOrigin = 'anonymous'; img.src = url; // 图片链接 img.onload = function () { const canvas = document.createElement('canvas') canvas.width = img.width; canvas.height = img.height; const ctx = canvas.getContext('2d'); ctx?.drawImage(img, 0, 0, img.width, img.height); const ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase(); const dataURL = canvas.toDataURL('image/' + ext); resolve(dataURL); } });
标签:src,canvas,const,img,url,base64,height,width,图片 From: https://www.cnblogs.com/atao24/p/18109963