如何在Appcelerator Titanium中获取远程图像的高度和宽度

时间:2017-10-12 09:31:42

标签: titanium appcelerator appcelerator-titanium

我发布以下示例代码,请帮助我

var bb_CoverPageImage = Ti.UI.createImageView({
    width : '100%',
    image : '',
    categoryName : magazinePagesList[i].categoryName,
    lengthId : magazinePagesList.length,
});

bb_CoverPageImage.image = L('Site_URL_Images') + magazinePagesList[i].imagePreviewUrl.replace(/\s/g, '%20');    

coverScrollView.add(bb_CoverPageImage);

1 个答案:

答案 0 :(得分:5)

Though you have not mentioned why you need image width/height of remote image, but there can be basically 3 ways to get the width/height of any remote image.


Solution 1:

You can either ask your backend devs to provide you the image width & height as separate parameters.


Solution 2:

You can use Ti.UI.ImageView's load event to wait till your image is loaded & then you can use a code similar like this:

imageview.addEventListener('load', function(e) {
    var image = imageview.toBlob();
    var h = image.height;
    var w = image.width;
});

This method forces that you can get width/height only after the image is loaded in ImageView.


Solution 3:

You can use web-service to download image & get further values:

var client = Ti.Network.createHTTPClient({
    onload: function(e) {
        var image = this.responseData;
        var h = image.height;
        var w = image.width;

        bb_CoverPageImage.image = image;
    },
    onerror: function(e) {}
});
client.open("GET", magazinePagesList[i].imagePreviewUrl);
client.send();