Search code examples
actionscript-3apache-flexflex4

Flex 4 - How to set alternate value for a spark Image control and Spark BitmapImage


How to set alternate value for Image and BitmapImage controls in flex 4 like HTML img tag

   <img src="url" alt="some_text">

But I want to display alternate image if the image is not correct.

Is it possible?


Solution

  • This will help you.

    The catch here is IOErrorEvent. If images not loading correctly it throws IO_ERROR EVENT (might be incorrect path). Also aware of Cross Domain Security issue.

    protected function img_creationCompleteHandler(event:FlexEvent):void
    {
        img.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void
        {
            img.source = "url";//Make sure it should be Right path;
        });
    }
    
    <s:Image id="img" toolTip="Hey tooltip here" creationComplete="img_creationCompleteHandler(event)" />