preView: function (url){
let reader = new FileReader();
me.getImageBlob( url , function(blob){
reader.readAsDataURL(blob);
});
reader.onload = function(e) {
//截取540*320的小图
var img2 = new Image();
img2.onload = function() {
canvas.width = 540;
canvas.height = 320;
// 清除画布
context.clearRect(0, 0, 540, 320);
// 图片压缩
context.drawImage(img2, 0, 0, 540, 320);
var type = 'image/jpeg';
//将canvas元素中的图像转变为DataURL
var dataurl = canvas.toDataURL(type);
document.getElementById("showPic").src=dataurl;
};
img2.src = e.target.result;
}
},
//通过url获取图片的Blob值(如果跨域,使用代理、服务器配置等方式)
getImageBlob : function (url, cb) {
var xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "blob";
xhr.onload = function() {
if (this.status == 200) {
if(cb) cb(this.response);
}
};
xhr.send();
}