如何确保 imageView 的远程图像将显示在带有滚动视图的窗口中?

发布于 2024-11-05 23:37:30 字数 2361 浏览 1 评论 0原文

我已经测试了很多东西来解决我的问题,但我现在不知道如何解决它。 在互联网上我发现 imageView 的“加载事件”可以工作,但事实并非如此。

我有以下代码:

var thisWin = Ti.UI.currentWindow;


// Sets the HTTP request method, and the URL to get data from
loader.open("GET","http://myserver/foo/")

// Send the HTTP request
loader.send();

// Runs the function when the data is ready for us to process
loader.onload = function() 
{   
    var rest_result = JSON.parse(this.responseText);

    var scrollView = Ti.UI.createScrollView({
        contentWidth: 'auto',
        contentHeight: 'auto',
        top:0,
        left:0,
        showVerticalScrollIndicator: true,
        scrollType: 'vertical',
        backgroundColor:'#fff'
    });

    var label_headline_general = Ti.UI.createLabel({
        color:'#0066cc',
        text:'My Headline 1',
        font:{fontSize:14,fontFamily:'Helvetica Neue'},
        shadowColor: '#bfd9f2',
        shadowOffset:{x:1, y:1},
        textAlign:'left',
        width:'auto',
        height:'auto',
        left:5,
        top:6
    });

    var label_headline_main = Ti.UI.createLabel({   
        color:'#003366',
        text:rest_result.name,  
        font:{fontSize:18,fontWeight:'bold',fontFamily:'Helvetica Neue'},
        shadowColor: '#ddeeff',
        shadowOffset:{x:1, y:1},    
        textAlign:'left',
        width:'auto',           
        height:'auto',
        left:5,
        top:label_headline_general.size.height + 5
    });

    // imageURI is something like: http://foo/myimage.png
    var imageURI = rest_result.image;

    if(imageURI == '')
    {
        imageURI = 'my-fallback-image.png';
    }

    var my_image = Ti.UI.createImageView({
        image:imageURI,
        defaultImage:'my-fallback-image.png',
        width:'auto',
        height:'auto',
        top:0,
        left:0
    });

    my_image.addEventListener('load',imageIsLoaded);

function imageIsLoaded(e) { 
    my_image.image = imageURI;
    my_image.show();
}

    scrollView.add(label_headline_general);
    scrollView.add(label_headline_main);
    scrollView.add(label_image);

    thisWin.add(scrollView);
}

在 Titanium 模拟器中,代码工作正常,并且如果 JSON 有图像,则会显示图像,但在设备上却不会。设备上显示默认图像。我认为我有一个计时问题(所有元素的渲染速度都比图像快)。

我在没有任何滚动视图的情况下使用窗口测试了相同的方法,并且它有效。在这种情况下,图像每次都会在每个设备上正确加载。

如何确保 imageView“my_image”中的远程图像加载到滚动视图中并将 100% 显示?有什么技巧吗?

谢谢!

I've tested a lot of things to solve my problem, but I have no idea now to fix it.
On the internet I found out, that the "load event" of the imageView could work, but it doesn't.

I have the following code:

var thisWin = Ti.UI.currentWindow;


// Sets the HTTP request method, and the URL to get data from
loader.open("GET","http://myserver/foo/")

// Send the HTTP request
loader.send();

// Runs the function when the data is ready for us to process
loader.onload = function() 
{   
    var rest_result = JSON.parse(this.responseText);

    var scrollView = Ti.UI.createScrollView({
        contentWidth: 'auto',
        contentHeight: 'auto',
        top:0,
        left:0,
        showVerticalScrollIndicator: true,
        scrollType: 'vertical',
        backgroundColor:'#fff'
    });

    var label_headline_general = Ti.UI.createLabel({
        color:'#0066cc',
        text:'My Headline 1',
        font:{fontSize:14,fontFamily:'Helvetica Neue'},
        shadowColor: '#bfd9f2',
        shadowOffset:{x:1, y:1},
        textAlign:'left',
        width:'auto',
        height:'auto',
        left:5,
        top:6
    });

    var label_headline_main = Ti.UI.createLabel({   
        color:'#003366',
        text:rest_result.name,  
        font:{fontSize:18,fontWeight:'bold',fontFamily:'Helvetica Neue'},
        shadowColor: '#ddeeff',
        shadowOffset:{x:1, y:1},    
        textAlign:'left',
        width:'auto',           
        height:'auto',
        left:5,
        top:label_headline_general.size.height + 5
    });

    // imageURI is something like: http://foo/myimage.png
    var imageURI = rest_result.image;

    if(imageURI == '')
    {
        imageURI = 'my-fallback-image.png';
    }

    var my_image = Ti.UI.createImageView({
        image:imageURI,
        defaultImage:'my-fallback-image.png',
        width:'auto',
        height:'auto',
        top:0,
        left:0
    });

    my_image.addEventListener('load',imageIsLoaded);

function imageIsLoaded(e) { 
    my_image.image = imageURI;
    my_image.show();
}

    scrollView.add(label_headline_general);
    scrollView.add(label_headline_main);
    scrollView.add(label_image);

    thisWin.add(scrollView);
}

In the Titanium-emulator the code works fine and the image, if the JSON had an image, were shown, but on the device it doesn't. On the device the default-image is shown. I think I have a timing problem (all elements are rendered faster than the image).

I tested the same approach with window and without any scrollView and it worked. In this case the image was loaded every time correct on every device.

How can I ensure, that the remote image in the imageView "my_image" is loaded in the scrollView and will be 100% shown? Is there any technique?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-11-12 23:37:30

根据这篇帖子,您可以观看加载< /code> 事件两次,尽管这看起来像是一个错误而不是功能。

According to this post you can watch for the load event twice though that almost seems like a bug rather then feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文