【问题标题】:Titanium: get png height without creating ImageViewTitanium:在不创建 ImageView 的情况下获取 png 高度
【发布时间】:2012-01-27 22:04:33
【问题描述】:

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

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

我想避免创建 ImageView,因为我要在获得 png 的高度并执行一些更改后创建 ImageView。

或者更确切地说,我将在 var imagevariablename = Ti.UI.createImageView 本身期间使用高度值,所以我不能使用 imagevariablename.height 因为尚未完成 var imagevariablename 的声明。

【问题讨论】:

    标签: image png height width titanium


    【解决方案1】:

    我正在处理这个问题,但上面的代码使用 3.2.2 时遇到了问题,在 Android 上进行测试。各种尝试只会给我 1、0 或 SIZE 的宽度和高度值。这确实使用了 imageView,但下面的内容让我在这个环境中得到了我需要的一切。我也使用加载事件而不是 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);
    });
    

    【讨论】:

      【解决方案2】:

      在我的情况下,这是这样运行的。

      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】:

        试试这个代码

        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;
        

        【讨论】:

          【解决方案4】:

          如果您从图像创建 Blob,则可以从 Blob 中获取宽度和高度

          来自 Titanium 文档: 如果此 blob 表示图像,则这是图像的高度(以像素为单位)。

          【讨论】:

          • 这为我返回 0。你知道它是如何判断是不是图片吗?
          【解决方案5】:

          我不知道如何在 Titanium 中不创建 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;
          

          【讨论】:

            猜你喜欢
            • 2012-06-25
            • 1970-01-01
            • 1970-01-01
            • 2018-01-15
            • 1970-01-01
            • 2018-11-17
            • 2014-09-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多