vue中将图片转为base64编码
methods:{
handleSave(){//保存图片到本地
var image = new Image();
var base64 = '';
image.src = document.getElementsByClassName('logo_pic')[0].src;
this.getBase64Image(image.src,function(res){
base64 = res
})
},
getBase64Image(url, callback, outputFormat){//图片转base64编码
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
img = new Image;
img.crossOrigin = 'Anonymous';
img.onload = function(){
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img,0,0);
var dataURL = canvas.toDataURL(outputFormat || 'image/png');
callback.call(this, dataURL);
canvas = null;
};
img.src = url;
},
saveImageCallback(res){
Toast('save success')
}
}
这里的图片路径不是相对路径