Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked August 12th 2010 by Conny Svensson
  • error
  • image
  • imageview
  • iphone
  • load
3 Comments
  • I'm also looking for this. My problem is worse, I don't get any error at all that the image failed loading. I tested it with a dummy image url.

    — commented October 16th 2010 by Shiki Shiji
  • I agree. An 'onerror' event would be very useful to have. Since we already have a 'load' event, it would make sense to catch an error. too.

    — commented December 22nd 2010 by Kosso
  • Ditto. I'd simply like to filter those Error messages out of the log.

    — commented April 11th 2011 by Robb Shecter

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.

    — answered December 22nd 2010 by Jim Carter III
    permalink
    0 Comments
  • 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);
    }
    
    — answered December 22nd 2010 by daniyal hamid
    permalink
    1 Comment
    • I tried that, the catch block is not called.

      — commented January 16th 2011 by Patrick Ryan
  • 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();
    
    — answered February 25th 2011 by Christophe Soudron
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.