views:

100

answers:

2

I have an image loader that I am attempting to resize dynamically. I would like to make sure that the aspect ratio is kept intact so that the image doesn't stretch. When I reload the image (re-populate it with another image via the loader), there is a resize function that is called. I would like to know what I might be doing wrong with this portion of the resize code:

var aspect:Number = width / height;
var cAspect:Number = _imageBoundary.width / _imageBoundary.height;

if (aspect <= cAspect) 
{
    _ldr.height = _imageBoundary.height;
    _ldr.width = aspect * _ldr.height;
}
else 
{
    _ldr.width = _imageBoundary.width;
    _ldr.height = _ldr.width / aspect;
}

_ldr.x = (_imageBoundary.width - _ldr.width) / 2 + _imageBoundary.x;
_ldr.y = (_imageBoundary.height - _ldr.height) / 2 + _imageBoundary.y;

Any thoughts would be greatly appreciated. I am basing the larger portion of this on bhups's code.

Best

A: 

I think the most easy and reliable way is to use the Image class. You can see a good example here: http://blog.flexexamples.com/2008/06/28/maintaining-an-images-aspect-ratio-on-an-image-control-in-flex/.

the Image class may get dynamically change in its source field and load the image accordingly.

Hope that helps.

yn2
Thanks for the reference-- Currently I am looking for an AS3 ONLY solution.
jml
+1  A: 

I believe the first line should be

var aspect:Number = _ldr.content.width / _ldr.content.height;
Hrundik
Beautiful. Thanks for spotting that.
jml