Unable to correctly perform the simplest task – fit image to screen. Pictures loads from the gallery, it is displays, but having tried many ways to understand that there are some oddities.
I use Flex ViewNavigatorApplication without ActionBar and full screen. Landscape screen orientation. The best option to accommodate the maximum size of image. All images have a size equal proportions of 3264 x 2448.
Do this:
protected var roll:CameraRoll;
protected var loader:Loader;
if(CameraRoll.supportsBrowseForImage && !roll)
{
roll = new CameraRoll();
roll.addEventListener(MediaEvent.SELECT, onSelect);
}
if(roll){
roll.browseForImage();
}
protected function onSelect(e:MediaEvent) : void
{
var data:MediaPromise = e.data;
var promise:MediaPromise = e.data as MediaPromise;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
loader.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onError);
loader.loadFilePromise(promise);
}
}
private function onImageLoaded(event:Event):void {
var bitmap:Bitmap = Bitmap(event.currentTarget.content);
var ratio:Number = bitmap.width / bitmap.height
trace (bitmap.width + '/' + bitmap.height + ' ratio: ' + ratio);//3264/2448 ratio: 1.3333333333333333
bitmap.width = Capabilities.screenResolutionY;
trace ('screenResolutionX: ' + Capabilities.screenResolutionY);//screenResolutionX: 800
bitmap.height = bitmap.width / ratio;
trace (bitmap.width + '/' + bitmap.height + ' ratio: ' + bitmap.width / bitmap.height);//799.9000000000001/599.95 ratio: 1.3332777731477623
spr.addChild(bitmap); //container
trace (spr.width + '/' + spr.height);//0/0
trace (this.width + '/' + this.height);//533/320 - here is the first - why these data?
trace (stage.width + '/' + stage.height);//1202.8500000000001/918.6500000000001 -
//here is the second - why these data?
//On the real phone in debug mode i see that image have about this dimensions, but trace above we see that the dimensions of 800 by 600.
trace (stage.stageWidth + '/' + stage.stageHeight);//800/480 - all OK
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onImageLoaded);
loader.contentLoaderInfo.removeEventListener(ErrorEvent.ERROR, onError);
}
The questions in the comments of traces. I do not understand why this is happening.
Since you are using Flex, have you tried using the Spark Image component instead of pure actionscript? It has a built in scaling with the
scaleModeandfillModeproperties. You might try something like this once your image has loaded: