get size of image titanium 2.0.2

You must Login before you can answer or comment on any questions.

Hello

Sorry for bad english.

I need get image width and height from image by following code:

var imageView = Titanium.UI.createImageView({
    height : 'auto',
    width : 'auto'
    image : //image to show (from database - memory - not in local file)
});
 
imageView.width //return real width of image
imageView.height //return real height of image 
imageView.size.height; //return 0
imageView.size.width; //return 0
In Titanium SDK 1.8.2 This work fine. But in Titanium SDK 2.0.2 not work.

in this case is image view fill to parent size. These mean that after render imageView.size.height and imageView.size.width have same value as parent size.

in 2.0 imageView.height return 'auto' and not real real height of image, in other words return same constant as is set when is imageview created.

-------ANOTHER TRY---------

I read the Migrating Applications to Release 2.0 in Migrating Applications to Release 2.0

And here it says : Any UI component with the width property defined as "auto" can safely define its width parameter as Ti.UI.SIZE to return to 1.8 behavior.

var imageView = Titanium.UI.createImageView({
    height : Ti.UI.SIZE,
    width : Ti.UI.SIZE
    image : //image to show
});
 
imageView.width //return 'SIZE' constant
imageView.height //return 'SIZE' constant
imageView.size.height; //return 0
imageView.size.width; //return 0
but it had the same effect.

-----ANOTHER TRY--------

I tried use 'postlayout' event of view to get view size when the view is rendered on the screen.

var imageView = Titanium.UI.createImageView({
    height : Ti.UI.SIZE,
    width : Ti.UI.SIZE
    image : //image to show
});
 
imageView.addEventListener('postlayout', function(e){
    imageView.width; //return 'SIZE' constant - but no real image width
    mageView.height; // return 'SIZE' constant - but no real image height
    imageView.size.height; //return imageview height on screen but no real image height
    imageView.size.width; //return imageview width on screen but no real image width
});
Thanks for help.

1 Answer

Accepted Answer

Hi Michal,

Try this.

imageView.toImage().width;
imageView.toImage().height;

— answered 11 months ago by Nitin Chavda
answer permalink
5 Comments
  • Thanks this works fine.

    — commented 11 months ago by Michal Lacko

  • You are most welcome.....

    — commented 11 months ago by Nitin Chavda

  • It's probably inefficient to call toImage() twice, how about caching it first?

    var i = imageView.toImage();
    i.width;
    i.height;

    — commented 11 months ago by Chris Barber

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!