钛:获取PNG高度而不创建ImageView

问题描述:

有没有办法在不创建ImageView的情况下获得.png高度?钛:获取PNG高度而不创建ImageView

我在google上找到的方法首先需要createImageView,然后再做一个.height。

我想避免创建ImageView,因为我在获取png的高度并执行一些更改后将创建imageImageView。或者更确切地说,我将在var imagevariablename = Ti.UI.createImageView本身中使用高度值,因此我无法使用imagevariablename.height,因为var imagevariablename的声明尚未完成。

我不知道有什么方法可以在不创建imageView的情况下获取图像的高度/宽度。在我的应用程序中,我创建了一个临时图像视图并读取了属性,而无需将其添加到视图/窗口中。

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; 
+0

它不适用于sdk版本2.1.1.GA – 2012-08-15 11:05:33

如果您从您的图像斑点,你可以得到的宽度和高度从团块

钛文档:然后,一旦你知道大小,您可以创建“真正”的图像视图 如果这个blob代表一个图像,这是以像素为单位的图像的高度。

试试这个代码

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; 

在我的条件,这是会像这样运行。

var imageTemp = Ti.UI.createImageView({ 
    image : someFile.read(), 
    height:'auto', 
    width:'auto' 
}); 
alert("height=" + imageTemp.toBlob().height); 
alert("width=" + imageTemp.toBlob().width); 
imageTemp = null; 

我正在研究这个问题,并且使用3.2.2在Android上进行测试时遇到了上述问题。各种尝试只会给我1,0或SIZE的宽度和高度值。这样做可以使用imageView,但以下功能可以在此环境中获得所需的一切。我也使用load事件而不是postLayout。希望这有助于某人。

$.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); 
});