获取本地的图片解决成 base64
getBase64(url, callback){var canvas = document.createElement("canvas"); // 创立 canvas DOM 元素,并设置其宽高和图片一样
var ctx = canvas.getContext("2d");
var img = new Image;
img.crossOrigin = 'Anonymous';
img.src = url;
console.log(url)
img.onload = function () {
canvas.height = 60; // 指定画板的高度, 自定义
canvas.width = 60; // 指定画板的宽度,自定义
ctx.drawImage(img, 0, 0, 60, 60); // 参数可自定义
var dataURL = canvas.toDataURL();
callback.call(this, dataURL); // 回掉函数获取 Base64 编码
canvas = null;
};
},
调用:
shareGame(){this.getBase64('../assets/logo.png',function (base64){console.log(base64)
let image = base64,
})
},