PDA

View Full Version : You're not listening to me!



letter
November 17th, 2009, 10:53 PM
Take a BIG user submited image: say 2848x2136 pixels (I said it was big) and resize it with a Matrix:



private static function resizeBitmapData(bmpSource:BitmapData, setWidth:Number, setHeight:Number):BitmapData {
var scaleWidth:Number=setWidth/bmpSource.width;
var scaleHeight:Number=setHeight/bmpSource.height;
var bmp:BitmapData=new BitmapData(setWidth,setHeight,true,0);
var matrix:Matrix = new Matrix ();
matrix.scale(scaleWidth, scaleHeight);
bmp.draw(bmpSource, matrix);
return bmp;
}



And then add the bitmap to the stage on the next line.... throws the error "can't access a property of a null... blah blah blah"


I get it, it's taking a while for flash to resize my gigantamus image, that's fine and understandable.


Finally to my question: How do I set up a listener (or something else) to make sure the image is actually resized.


I tried firing a method inside my resizer right before the bmp is returned, but that didn't seem to work. Only thing that works is a manual timer to slow things down a bit and that's just messy.


I guess I'm wishing for a bm.draw(bmd).addEventListener(GRAPHICSEVENT.comple te, doneResizing); or something crazy like that. But I can find no such event in the package.


Other ideas for making sure flash is done with bitmap data before adding it to the display list?

justkevin
November 18th, 2009, 12:08 AM
Since Flash doesn't have internal asynchronous operations, what you're asking for doesn't exist. Flash won't hit the return statement until bmp.draw() is done-- there's nothing happening "in the background" to wait on (there's an exception to this for actions that are relying on external processes like network events).

If your function is returning null, then it's a problem of bmp being null when drawing is done. This is probably the result of the hard limit Flash has on BitmapData, which I believe is 2880 pixels. Are you scaling up or down? If it's up, then Flash won't be able to create the target bmp.

letter
November 18th, 2009, 09:01 AM
Thanks for the reply Kevin, I'm scaling down, but I actually just figured it out:
/// before matrix transformation
bm.lock();
/// after returning the bitmap
bm.unlock();

and the bitmap shows up fine, no errors.

And yes, I should have read about this in the language ref:

lock (http://www.kirupa.com/forum/#lock())():void (http://www.kirupa.com/forum/../../specialTypes.html#void)
Locks an image so that any objects that reference the BitmapData object, such as Bitmap objects, are not updated when this BitmapData object changes.

unlock (http://www.kirupa.com/forum/#unlock())(changeRect:Rectangle (http://www.kirupa.com/forum/../../flash/geom/Rectangle.html) = null):void (http://www.kirupa.com/forum/../../specialTypes.html#void)
Unlocks an image so that any objects that reference the BitmapData object, such as Bitmap objects, are updated when this BitmapData object changes.

BeerOclock
November 18th, 2009, 10:34 AM
I thought that lock and unlock were really just for performance. For example if you are making 100 changes to a bitmapData object, you would lock it first and unlock it after.

I didnt think that it would make the difference between crashing the program... ?

birdwing
November 18th, 2009, 11:06 AM
with the size of the image that was being resized it would be improper to NOT use lock() unlock(). when to use it is based on how much the processor is being used, not on how many things you are doing.

AHernandez
November 18th, 2009, 06:29 PM
There is a maximum width or height of 2880 on BitmapData.