tags:

views:

38

answers:

2

I have a barCode class that is used to generate an image of a barCode. I create an instance of this class and it works as expected for example:

var myBarCodeInstance:barCode = new barCode();

var myBarCodeImg:Image = new Image();
myBarCodeImg.source = myBarCodeInstance;

Using this code the image appears and works fine. However, my question is how do I implement a loader on this image that will fire an event when the image is fully loaded and ready for processing? (I am running into null problems with the image not being fully loaded before attempting to access its contents).

Something like the below:

var loader:Loader;

loader = new Loader();

loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Ev ent):void{
myBarCodeImg.source = e.currentTarget.content;
// further processing here
});

loader.load(new URLRequest(encodeURI(“image.jpg“)));

but i dont know what to insert in place of the "image.jpg" part due to my image being an instance of a class and not an actual jpg file.

A: 

Image dispatches a complete event - try listening for that event:

var myBarCodeInstance:BarCode = new BarCode();
var myBarCodeImg:Image = new Image(); 
myBarCodeImg.addEventListener(Event.COMPLETE, completeHandler);
myBarCodeImg.source = myBarCodeInstance;

function completeHandler(e:Event):void
{
    var img:Image = e.currentTarget as Image;
    var barcode:BarCode = img.content as BarCode;
    /* process it */
}
Amarghosh
I tried something similar see the new code I just posted, can you tell me what is worng and how to fix it? Thanks for your help so far, cheers.
scott
A: 

Hi, i tried something similar to your suggestion below but the trace text 'image loaded' is never displayed when the button is clicked even though the image appears with no problems. This is an AIR app if it makes any difference, Does the code below work for you? If not can you see whats wrong?Thanks.

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>


<fx:Script>
<![CDATA[

import mx.controls.Image;
import mx.events.FlexEvent;
[Embed(source="../images/logo.jpg")]
[Bindable]
public var logoImg:Class;


var tempImg:Image;

protected function testBut_clickHandler(event:MouseEvent):void
{

trace('button pressed')
tempImg = new Image();
tempImg.addEventListener(Event.COMPLETE, function(e:Event):void{ trace('IMAGE LOADED'); } )
tempImg.source = logoImg;
addElement(tempImg);


}

]]>
</fx:Script>

<s:Button x="86" y="14" label="test" id="testBut" click="testBut_clickHandler(event)"/>

</s:WindowedApplication>
scott