I am writing an application that should batch resize images in a folder. My problem is that my code works for directories that has small images or directories that has small number of images, but when I run my code on directories where there are many large images that requires more processing, the application crashes without reporting any errors. Here is my code:
function BatchResizeDirectory(){
var dir : File = new File;
dir.addEventListener(Event.SELECT,function(){
var files = dir.getDirectoryListing();
for each (var file:File in files)
{
if(["png","jpg","bmp","jpeg"].indexOf(file.extension.toLocaleLowerCase()) != -1){
file.addEventListener(Event.COMPLETE,function(e:Event){
var f:File = File(e.target);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event){
//init bmp data
var bmpd:BitmapData = new BitmapData(loader.content.width,loader.content.height);
bmpd.draw(loader);
//resize
var result:BitmapData = ImageResizer.bilinearIterative(bmpd, 64,64,
ResizeMath.METHOD_PAN_AND_SCAN);
//write to disk
var pnge:JPEGEncoder = new JPEGEncoder();
var bytes:ByteArray = pnge.encode(result);
var name = f.name.replace("."+f.extension,"");
var outputFilename = dir.nativePath+"/"+ name + "_resized." + f.extension;
var outputFile:File = dir.resolvePath(outputFilename);
var fs:FileStream = new FileStream();
try{
//open file in write mode
fs.open(outputFile,FileMode.WRITE);
//write bytes from the byte array
fs.writeBytes(bytes);
//close the file
fs.close();
}catch(e:Error){
trace(e.message);
}
});
loader.loadBytes(f.data);
});
file.load();
}
}
});
dir.browseForDirectory("Choose the input folder");
}
My question is. Why is my application crashing on heavy processing? Does it have anything to do with anonymous declaration of event handlers? I am confused.
Thank you!
Check your memory usage, also you can call bitmapData.dispose
once you are done with resizing an image to free the memory.
I would also move those anonymous functions out to make it easier to read and less prone to error due to the for loop.
Edit : I believe the problem was that the files array had no other refence so you were losing the references to the files. Try the following code and see if it helps.
package
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.filesystem.File;
import flash.filesystem.FileStream;
public class Main extends Sprite
{
private var _files:Array;
public function Main():void
{
BatchResizeDirectory();
}
private function BatchResizeDirectory():void{
var dir : File = new File;
dir.addEventListener(Event.SELECT,function():void{
_files = dir.getDirectoryListing();
for each (var file:File in _files)
{
loadFile(file);
}
});
dir.browseForDirectory("Choose the input folder");
}
private function loadFile(file:File):void
{
if(file.extension && ["png","jpg","bmp","jpeg"].indexOf(file.extension.toLocaleLowerCase()) != -1){
file.addEventListener(Event.COMPLETE, onComplete);
file.load();
}
}
private function onComplete(e:Event):void
{
var f:File = File(e.target);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT,function(e:Event):void{
trace(f.name);
});
loader.loadBytes(f.data);
}
}
}