我有im64.onload方法的base64图像数组来调整它们的大小,我通过img.width和img.height传递宽度和高度,但我总是以最后一个图像的相同宽度和高度结束。
我尝试了这个答案JavaScript loop/scope issue with Image.onload() 不适合我
var list = myimgs.length;
var base64List = [];
var myJSON;
if(list != 0){
for(var i = 0; i <list; i++){
var count = 0;
var img = new Image;
img.onload = resizeImage();
img.src = myimgs[i].src;
img.width= myimgs[i].width;
img.height= myimgs[i].height;
function resizeImage() {
if((document.getElementById('editor').innerHTML.indexOf("base64") != -1)){
emailBody = emailBody.replace(/<img src="data:image[^>]*>/g,"imageTag");
var x = img.width;
var y = img.height;
var newDataUri = imageToDataUriWithoutChangingEquality(this, x, y);
var base64 = newDataUri.match(/data:(.+);base64,(.+)/);
var base64Number = Math.round((base64[2].length )*3/4);
var dateTime = new Date().toLocaleString()
var tmpDocument = {
sobjectType: "Document",
Name: 'Image ' +dateTime,
ContentType: 'image/png',
Body: base64[2]
};
base64List.push(tmpDocument);
console.log(JSON.stringify(tmpDocument));
myJSON = JSON.stringify(base64List);
}
count++;
}
}
}