Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

How can I get the Height and Width dimentions of an Image?

Hi there,

I am trying to get the information about the height and width of an Image object. I found something reallted to toImage(), but coulding figure out how to use it.

Any clues?

— asked May 23rd 2010 by Antonio Silveira
  • image
  • size
0 Comments

8 Answers

  • Accepted Answer

    If it is an image you are using, put it in an ImageView, then you can get the height and width. You need to set the height / width of the image view to 'auto' or the properties will be zero.

    var image1 = Ti.UI.createImageView({
     // use whatever image you have here
      image: Ti.Filesystem.applicationDataDirectory + Ti.Filesystem.separator +'E1VIPNCH.gif',
      height: 'auto',
      width: 'auto'
    });
    
    Ti.API.debug(image1.size.width + "x" + image1.size.height);
    Ti.API.debug(image1.width + "x" + image1.height);
    
    — answered October 22nd 2010 by Jeff Bonnes
    permalink
    5 Comments
    • That's it, it worked. Thanks Jeff.

      — commented October 28th 2010 by Antonio Silveira
    • Doesn t seem to work on android :/ (works on iphone).Titanium 1.7.2

      — commented August 12th 2011 by jb cazaux
    • Why doesn't this work on Android? It gives 0x0 or autoxauto.

      — commented October 28th 2011 by 1 2
    • THIS DOES NOT WORK ANY MORE ON 2.0!!!
      Is there ANY other way to obtain an image widht/height?

      — commented September 28th 2012 by Ronald van Woensel
    • 4 years, and still no right/final solution to this problem!!!

      — commented September 8th 2014 by noura rizk
  • I was working on this and had trouble with the code above using 3.2.2, testing on Android. Various attempts would just give me 1, 0 or SIZE for the width and height values. The following gets me everything I need in this environment. I also am using the load event instead of postLayout. Hopefully this helps someone.

        $.map.image = 'http://getyourownimage.com/dev/8fac94c6-872b-4bda-a56a-7dba09188c66.png';
        $.map.zIndex = 1;
        $.map.width = 'auto';
        $.map.height = 'auto';
    
        $.map.addEventListener('load',function(e){
            var rect = $.map.getRect();
            Ti.API.info(rect.width); //actual width of imageView
            Ti.API.info(rect.height); //actual height of imageView    
            Ti.API.info($.map.getWidth()); //returns auto/SIZE, doesn't work
            Ti.API.info($.map.getHeight()); //returns auto/SIZE, doesn't work
            Ti.API.info($.map.toImage().width); //some scaled value, not useful
            Ti.API.info($.map.toImage().height); //some scaled value, not useful
            Ti.API.info($.map.toBlob().width); //image original/full size width
            Ti.API.info($.map.toBlob().height); //image original/full size height        
            alert('rectX:'+rect.width+',rectY:'+rect.height+',mapGW:'+$.map.getWidth()+',mapGH:'+$.map.getHeight()+',tiX:'+$.map.toImage().width+',tiY:'+$.map.toImage().height+',tbX:'+$.map.toBlob().width+',tbY:'+$.map.toBlob().height);
        });
    
    — answered April 1st 2014 by Dave Stewart
    permalink
    4 Comments
    • and yes, creating a variable for the toBlob() result and only calling this once would be more efficient

      — commented April 1st 2014 by Dave Stewart
    • This actually works, but I think I can't use it with dummy imageView just to get width and height right?!

      — commented September 8th 2014 by noura rizk
    • You should be able to do with visible set to false? I have not tried this.

      — commented September 8th 2014 by Dave Stewart
    • hmmm, will not work in my case, but at least I've a reference now.
      Thanks Dave..

      — commented September 8th 2014 by noura rizk
  • var imageTemp = Ti.UI.createImageView({
        image:'somefile.png',
        height:'auto',
        width:'auto'
    });
    var imgWidth = imageTemp.toImage().width;    
    var imgHeight = imageTemp.toImage().height;    
    imageTemp = null;
    
    — answered December 30th 2012 by d s
    permalink
    4 Comments
    • This method always gives me the size as 100x100.
      Ti SDK version 3.0.2

      — commented February 27th 2013 by Dhirendra Pratap
    • Omitting the auto properties seems to fix it for me:

      var imageTemp = Ti.UI.createImageView({ image: 'images/star.png' });
      var starWidth = imageTemp.toImage().width;
      

      I just retested this code on 3.0.2 and it works fine. I tested with 3 different images of different dimensions.

      — commented February 27th 2013 by d s
    • I also tested with the auto properties and it works fine too. However. auto has been deprecated anyway so you really shouldn't use it now.

      What code are you using?

      — commented February 27th 2013 by d s
    • Thanks a lot ! It worked for me:

      var imageTemp = Ti.UI.createImageView({ image: 'images/star.png' });
      var starWidth = imageTemp.toImage().width;

      — commented March 23rd 2013 by Samavaya Samavaya
  • This method is broken in 2.0. widht/height is nan, size.width and size.height is 0/0
    so this workaround is broken. A very fundamental feature is not available any more.

    — answered September 28th 2012 by Ronald van Woensel
    permalink
    0 Comments
  • hi there,
    this doesn't work on 2.0.2 GA what can i do?

    — answered July 15th 2012 by Graham Jeffrey
    permalink
    0 Comments
  • I spent some time looking for the way to get image width and height from image path there are many post out there but the only way that work for me is this. "Android" test only. Hopefully this is going to help other

    for (var i = 0; i < sb.SelectedImages.length; i++) {                
    imagesSize += Ti.Filesystem.getFile(sb.SelectedImages[i].image.nativePath).size;
    
        var imagePath = sb.SelectedImages[i].image.nativePath; 
        var imgView = Ti.UI.createImageView({
                        image : imagePath,
                        width : 'auto',
                        height : 'auto'
                    }); 
    
                    var image = imgView.toImage();
    
                    if (image.width > 1024) {
                        image.width = 1024;
                    }
                    if (image.height > 768) {
                        image.height = 768;
                    }
    
                    images.push(imagePath);
    }
    
    — answered November 24th 2012 by Nu Maniphanh
    permalink
    0 Comments
  • Antonio:
    I was looking for the same thing when I came across your question. I think you'll find how to do this in the answer to the following:

    http://developer.appcelerator.com/question/40281/maptoimage-help

    –T

    — answered August 8th 2010 by Ted Haeger
    permalink
    0 Comments
  • hey you can set the image to be appear in a particular size just by setting its margin property. use like below and set as per your need
    top:5,
    bottom:250,
    left:30,
    right:150
    Hope it will help!!

    — answered May 24th 2010 by Amit Srivastava
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.