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?
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);
-
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); });
-
var imageTemp = Ti.UI.createImageView({ image:'somefile.png', height:'auto', width:'auto' }); var imgWidth = imageTemp.toImage().width; var imgHeight = imageTemp.toImage().height; imageTemp = null;
-
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. -
hi there,
this doesn't work on 2.0.2 GA what can i do? -
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); }
-
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
-
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!!