Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Highlighted
New Developer
Posts: 47
Registered: ‎10-22-2009
My Device: Not Specified
Accepted Solution

Get height and width of an image

So i have a few images in my program that i am loading. These images are much longer than the screen size. So i am putting it in a ScrollView. My question is how do i get the height and width of an image. Doing the image.width and image.height does not seem to return the correct values.

 

 

//add the image
private function addImage():Container {
	var sp1:Container = new Container();
	sp1.sizeUnit = SizeUnit.PIXELS;
	sp1.debugColor = 0xFFFFFF;
	sp1.margins = Vector.<Number>([1,0,0,0]);
	sp1.align =ContainerAlign.NEAR;
	//sp1.setSize(650, 800);
	
	var myPattern:RegExp = / /g;
	var fileName:String = "./com/swiz/img1.png";
	var icon:Image = new Image();
	icon.setImage(File.applicationDirectory.resolvePath(fileName).url);
	//icon.setPosition(0,0);
	//icon.setSize(320,800);
	sp1.addChild(icon);
	sp1.setSize(650, icon.height);

	return sp1;
}

 

 

and my scrollview code is 

 

 

//add to the scrollView
var scroller:ScrollPane = new ScrollPane();
scroller.width = 650; 
scroller.height = 505;
scroller.y = 0; 
scroller.addScrollContent(addImage()); 
addChild(scroller);

 

Thanks in advance!

 

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Get height and width of an image

hey shethab,

 

in order to retreive the height and width of you image, you must use utilize the an event listener on your image and check for the Event.COMPLETE event. what happens is becasue the image you are loading is not embedded, it takes time to load into your application via the setImage() method. and when you are checking for the heigh width after the setImage method, the image hasnt been loaded and has no height and width at that second. so if you use the event listener, you can retreive the height and width since it has been finally loaded.

 

try the following code:

 

 

import flash.events.Event;

//add the image
private function addImage():Container {
	var sp1:Container = new Container();
	sp1.sizeUnit = SizeUnit.PIXELS;
	sp1.debugColor = 0xFFFFFF;
	sp1.margins = Vector.<Number>([1,0,0,0]);
	sp1.align =ContainerAlign.NEAR;
	//sp1.setSize(650, 800);
	
	var myPattern:RegExp = / /g;
	var fileName:String = "./com/swiz/img1.png";
	var icon:Image = new Image();
	icon.setImage(File.applicationDirectory.resolvePath(fileName).url);
	icon.addEventListener(Event.COMPLETE, onImageLoaded);
	//icon.setPosition(0,0);
	//icon.setSize(320,800);
	sp1.addChild(icon);
	sp1.setSize(650, icon.height);
	
	return sp1;
}

private function onImageLoaded(e:Event):void
{
	trace("image height: " + e.target.height + " image width: " + e.target.width);
}

 

hope that clears somethings up. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
New Developer
Posts: 47
Registered: ‎10-22-2009
My Device: Not Specified

Re: Get height and width of an image

You sir are a coding ninja Smiley Happy

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Get height and width of an image

haha thanks always wanted to be a ninja! Smiley Very Happy

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman