How to catch an image loading error
I'm building an RSS feed window as part of my app. The feed items references images which unfortunately sometimes are broken. I want to show these images in the feed list but for the broken images I want to hide them instead of showing the default image. In the console I get the following error
[ERROR] Failed to load image: http://www.alingsastidning.se/sidbilder/litenbild_20100811124141nyhet.jpg, Error: Error Domain=com.rssreader._rssreader.imageloader Code=1 "Response returned nil" UserInfo=0x7a1eda0 {NSLocalizedDescription=Response returned nil}
I would like to catch this error in my code so I could then hide the image. I don't like showing default grey images as it looks broken (which it is…).
I have searched the forum and the docs but couldn't find a way to get a hold of this error. Would be nice if it was simply an error event posted on the imageview object.
Thanks for any help in this matter
3 Answers
-
I would love to see this on the roadmap too. My use case is where i'm loading images where they are redirects (tumblr does this on large images so the urls are friendly). the only thing i've see that may be a workaround is where you run a toBlob() and assign a timeout, but i really don't want to take the memory hit when loading tons of images in.
-
did you try the native JavaScript try/catch? In catch try outputting the error object (the argument to catch) to the console like:
try { .. } catch(e) { Ti.API.info(e); }
-
Hi,
Maybe you can try to use a HEAD httpRequest at the same time you're trying to load your image, to check the state of the picture on the server. It will cost a little more bandwidth, but not that much because it's only a few bytes (you're not downloading the picture twice, but just asking the server what's the status of the file).
xhr = Titanium.Network.createHTTPClient(); xhr.setTimeout(10000); xhr.onload = function() { if (this.status != 200) { somethingsWrong_function(); } else { everythingsOK_function(); } }; xhr.onerror = function(e) { somethingsWrong_function(); }; xhr.open("HEAD", "http://www.blahblah.com/pictures/blah.png"); xhr.send();