Titanium: get png height without creating ImageView
Asked Answered
B

5

8

Is there a way to get a .png height without creating ImageView?

The methods I found on google require createImageView first and then doing a .height.

I want to avoid creating ImageView because I'm going to createImageView after I get the height of the png and perform some changes.

Or rather, I will be using the height value during the var imagevariablename = Ti.UI.createImageView itself, so I can't using imagevariablename.height because the declaration of var imagevariablename is not done yet.

Bunn answered 26/12, 2011 at 10:16 Comment(0)
J
5

I don't know of any way to get the height / width of an image without creating an imageView in Titanium. In my app, I create a temporary image view and read the attributes without ever adding it to a view / window. Then you can create the 'real' image view once you know the size:

var imageTemp = Ti.UI.createImageView({
  image : someFile.read(),
  height:'auto',
  width:'auto'
});
Ti.API.info( "height=" + imageTemp.size.height);
Ti.API.info( "width=" + imageTemp.size.width);
imageTemp = null;
Jostle answered 27/12, 2011 at 11:10 Comment(0)
I
5

Try this code

var imageTemp = Ti.UI.createImageView({
    image : <image>,
    height:'auto',
    width:'auto'
}),
imageSize = imageTemp.toImage();

Ti.API.info( "height=" + imageSize.height);
Ti.API.info( "width=" + imageSize.width);

imageTemp = imageSize = null;
Ilka answered 21/11, 2012 at 10:8 Comment(0)
S
1

If you create a Blob from your image you can get width and height from the blob

From Titanium docs: If this blob represents an image, this is the height of the image in pixels.

Slagle answered 19/8, 2012 at 10:23 Comment(1)
This returns 0 for me. Do you know how it determines if it's an image?Illustrate
N
1

In my condition this is run like this.

var imageTemp = Ti.UI.createImageView({
  image : someFile.read(),
  height:'auto',
  width:'auto'
});
alert( "height=" + imageTemp.toBlob().height);
alert( "width=" + imageTemp.toBlob().width);
imageTemp = null;
Natashianatassia answered 8/1, 2013 at 12:53 Comment(0)
B
1

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. This DOES use an imageView, but 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);
});
Bannockburn answered 1/4, 2014 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.