img.onload没有保持相同大小的javascript

时间:2018-04-17 17:22:43

标签: javascript image canvas html5-canvas onload

我有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++;
      }
    }
  }

0 个答案:

没有答案