PDA

View Full Version : Bulk Reading/Writing images stalls app


midimid
04-24-2009, 03:47 PM
I'm looping through a bunch of images and writing them to the desktop from the web - its a long story, but basically, each file is checked if it exists locally, loaded from a URL if not, and then written to the desktop using JPGEncoder.

As far as I can tell, during the saving process, the app is stalling.

I have a simple MOUSE_MOVE function that keeps a sprite following the mouse. During the saving process, the sprite does NOT move.

Here's my save function. I'm careful to do nothing else during this function, and only continue after the end of it.

{edit} - I've learned through some extra tracing, that the spot its stopping at is jpgEncoder.encode(). Note that imageRawData.imageRawData.content.bitmapData is just coming straight from a Loader Class.

Why is the encoder so slow? Is there any other way to do this?

If I don't need a user to see these images themselves, do I even need to do this encode?


public function saveImage(imageRawData):void {
trace("SAVING IMAGE")
var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(imageRawData.imageRawData.conten t.bitmapData);
var imageFile:File = imageRawData.image.localFile;
var filestream:FileStream = new FileStream();
filestream.open(imageFile,FileMode.WRITE);
filestream.writeBytes(jpgStream);
filestream.close();
}

kukoc
04-28-2009, 08:49 AM
its normal that image converting take some time becouse it needs a lot of CPU power, especjaly if jpg files have large resolution. for me better way is server side resizing before download and cache them on server hdd.

but when every time you need different resolution you need to do it just as you show.
you can try to move the jpg encoding to the different thread and make it asynchronic.

here is tutorial i googled:
http://blog.inspirit.ru/?p=201
google cache:
http://209.85.229.132/search?q=cache:KUepX3qGib4J:blog.inspirit.ru/%3Fp%3D201+JPGEncoder+synchronic&cd=1&hl=pl&ct=clnk&gl=pl&client=firefox-a

midimid
04-28-2009, 12:16 PM
Actually - found an excellent solution here by Jerion. (http://www.actionscript.org/forums/showpost.php3?p=691733&postcount=4) Honestly I can deal with the delay in time, but what I could not deal with was the stalling of the app. Jerion's solution adds event listening, and fixes the issue.

its normal that image converting take some time becouse it needs a lot of CPU power, especjaly if jpg files have large resolution. for me better way is server side resizing before download and cache them on server hdd.

but when every time you need different resolution you need to do it just as you show.
you can try to move the jpg encoding to the different thread and make it asynchronic.

here is tutorial i googled:
http://blog.inspirit.ru/?p=201
google cache:
http://209.85.229.132/search?q=cache:KUepX3qGib4J:blog.inspirit.ru/%3Fp%3D201+JPGEncoder+synchronic&cd=1&hl=pl&ct=clnk&gl=pl&client=firefox-a