Simple image refresh
I've tried a dozen ways to refresh a createImageView image, but nothing seems to work. The filename of the image will not change. I just want to reload it every X seconds.
Suggestions?
7 Answers
-
Just try something like this:
var loadImage = function() { var seed=Math.random(); url = "http://host/dir/image.jpg?"+seed; image.image = url; }; loadImage(); setInterval(loadImage,10000);
-
I can get an image to load that way, but how do I force a refresh of that image?
-
As a workaround, I decided to use a WebView instead of an ImageView. It's not the way I want to do it, but in the HTML I can actually put the Javascript in to refresh the image.
I would still love a way to do this in ImageView!
-
I just wrote this and it works on both iPhone and Android.
// this sets the background color of the master UIView (when there are no windows/tab groups on it) Titanium.UI.setBackgroundColor('#000'); // create tab group var tabGroup = Titanium.UI.createTabGroup(); var image1 = 'image1.png'; var image2 = 'image2.png'; // // create base UI tab and root window // var win1 = Titanium.UI.createWindow({ title:'Tab 1', backgroundColor:'#fff' }); var tab1 = Titanium.UI.createTab({ icon:'KS_nav_views.png', title:'Tab 1', window:win1 }); var img = Titanium.UI.createImageView({ width:100, height:100, top:50, left:110, url:image1 }); win1.add(img); var btn = Titanium.UI.createButton({ title:'load', width:100, height:35, top:50, left:0 }); function switchImage(){ if(img.url == image1){ img.url = image2; } else { img.url = image1; } } btn.addEventListener('click',function(){ switchImage(); }); setInterval(switchImage,3000); win1.add(btn); // // add tabs // tabGroup.addTab(tab1); // open tab group tabGroup.open();
-
I am having issues with this setInterval thing. Can someone help? I have set up an xhr calling a JSON service. No problems getting data, no problem calling data every 10 seconds with set interval, the issue is that every 10 seconds the data is called it doesn't "replace" the last data, visually, it adds it on top of the data already there. So it looks like my data text gets bolder and bolder every 10 seconds. Any suggestions?
-
In my case I'm trying image to reload every time I download a new image.
So instead of downloading picture into file and setting ImageView "image" property to filename, I just set "image" property to BLOB data I recieve from server - that helps in my case.
P.S.:
-
But anyway I had to use two ImageViews and move one above another (using zIndex) each time a picture changes - to prevent bliking. -
You should be able to just set the url property with the new image url.
var image = Ti.UI.createImageView();
image.url = "my.png";