4
我正在採取在該地區下降的文件列表。然後獲取他們的dataUrl並將它們放入Img中。然後使用drawImage
將該圖像放在畫布上。我根據它們的大小適當縮放圖像。它的功能正常。但問題在於操作完成後。例如在Canvas被附加到dom CPU並且內存使用率變高之後很久。只是爲了顯示畫布,它需要高CPU和內存。畫布甚至Img吃RAM和CPU
代碼降事件中:
e.preventDefault();
$("#controller_search").attr('value', '^')
$("#controller_search").attr('disabled', 'disabled');
$("#imageList").html('');
var templateData = "\
<div class='imageviewer-up'> \
<div class='curtain'></div> \
<canvas class='canvas'></canvas> \
<div class='loading'>0%</div> \
</div> \
";
for(var i=0;i<event.dataTransfer.files.length;++i){
var file = event.dataTransfer.files[i];
var reader = new FileReader();
reader.onload = (function(file){
return function(e){
var template = $(templateData);
var image = new Image();/*template.find('img')[0];*/
image.onload = (function(image){
return function(){
var size = {height: image.height, width: image.width}
var rSize = size;
if(size.height > 175)
rSize = {height: 175, width: (175*size.width)/size.height}
else{
rSize = {height: 175/(size.width/size.height), width: size.width/(size.width/size.height)}
}
image.height = rSize.height;
image.width = rSize.width;
var canvas = template.find('canvas')[0];
var context = canvas.getContext("2d");
context.mozImageSmoothingEnabled = true;
canvas.height = 175+4;
canvas.width = image.width+4;
context.drawImage(image, (canvas.height-image.height)/2, 2, image.width, image.height);
template.css('height', 175+4+0);
template.css('width', image.width+0);
}
})(image)
image.src = e.target.result;
image.title = file.name;
console.log(file.name);
$("#imageList").append(template);
image = null;
}
})(file);
reader.readAsDataURL(file);
}
目前我通過拖動20幅圖像(總計爲45 MB),所以20個帆布被創建測試。這太多了嗎?
編輯
我用,而不是帆布IMG甚至測試。例如僅使用IMG和dataUrls。但也是這樣。以同樣的方式放慢速度。那麼我的代碼中是否有內存泄漏?