如何确保 imageView 的远程图像将显示在带有滚动视图的窗口中?
我已经测试了很多东西来解决我的问题,但我现在不知道如何解决它。 在互联网上我发现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据这篇帖子,您可以观看
加载< /code> 事件两次,尽管这看起来像是一个错误而不是功能。
According to this post you can watch for the
load
event twice though that almost seems like a bug rather then feature.