Adobe AIR - How can I catch the event of image loading completed? - apache-flex

Core of my code is following:
var img:Image = new Image;
img.source = 'http://..........';
img.autoLoad = true;
img.cachePolicy = 'on';
img.addEventListener(Event.COMPLETE, function(event:Event):void {
trace('Loaded!', img.source);
});
img.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event:Event):void {
trace('Error!', img.source);
});
img.addEventListener(IOErrorEvent.IO_ERROR, function(event:Event):void {
trace('Error!', img.source);
});
I found that, the complete event does not occur for some images.
How can I catch complete event without signal leaks?

When you want to load an image (or even another swf) the class to use is Loader. A quick example:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handlerFunction);
loader.load(new URLRequest("http://somewhere/image.png"));
The only kind of tricky thing is that the events related to the loading are dispatched by the loader.contentLoaderInfo object, not the loader object.
And the always handy documentation:
Loader Class

Related

FLEX - Load an image of type class instance

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.
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 */
}

AS3: Socket timeout

I get a vague "Socket timeout." error on occasion when I am loading my site. I make various HTTP requests for PHP data and also am using a Loader() class instance. Can anyone shed some light on where this error might be coming from?
I wish there was more of an indication of where the error stemmed from...
Here is my code that I am using.
There are multiple problems going on, but the most important is that catch{} catches an error on first load. I have a fade in function that only works if the loader is fully loaded and I know that all of my URL links work, so it can't be that.
public function loadImage(url:String):void
{
this._imageURL = url;
this.alpha = 1.0; //need this because we might have just faded the image out
_ldr.alpha = 0.0;
_prog.alpha = 1.0;
_sqr.alpha = 0.0;
try
{
_ldr.close();
_ldr.unload();
}
catch(e:Error)
{
trace("error in bmdisplay: " + e.message);
}
if(!_imageURL)
{
return;
}
_loaded = false;
_ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
_ldr.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
_ldr.load(new URLRequest(_imageURL));
}
Could you provide more precise information on what you're actually loading and how?
You should be able to get the exact request throwing the error by listening for the securityError and ioError events on the contentLoaderInfo of the Loader object.
Something like this:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorListener);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorListener);
var request:URLRequest = new URLRequest(url);
loader.load(request);
...
private function errorListener(event:Event):void {
var url_causing_the_error:String = LoaderInfo(event.target).loaderURL;
...
}

AS3 Passing and getting data to ASP

I've been researching for days on the issude but till now I still haven found a solution yet.
I have 0 knowledge on ASP. And I just want to able to pass and get var/text from ASP.
Anyone kind enuff to guide me how I can furthur from here?
private function loadASP():void {
var aspSend:URLRequest=new URLRequest("testASP.asp");
var aspLoader:URLLoader = new URLLoader();
aspLoader.load(aspSend);
trace("did send");
//aspLoader.addEventListener(Event.COMPLETE, processASP);
}
private function processASP(e:Event):void {
}
Why have you commented the call to addEventListener method? Uncomment it (and move it up two lines so that it comes before the load call). If the url is correct, the processASP method will be called when the response arrives (in a real life application, make sure you listen for ioError and securityError on the URLLoader - check the link for examples on doing this). You can read the response as e.target.data in the processASP method.
private function processASP(e:Event):void
{
var loader:URLLoader = URLLoader(e.target);
trace("Response is " + loader.data);
}
URLLoader can also be used to send data to the asp page (server).
var ldr:URLLoader = new URLLoader();
var data:URLVariables = new URLVariables();
data.something = "someData";
data.somethingElse = "moreData";
var request:URLRequest = new URLRequest("url.asp");
request.data = data;
request.method = URLRequestMethod.POST;//or GET
ldr.addEventListener(Event.COMPLETE, onLoad);
//listen for other events
ldr.load(request);

How to resolve Adobe Flex error: "Error #2036: Load Never Completed"?

How to resolve Adobe Flex error: "Error #2036: Load Never Completed"?
Don't forget you can also add an IOErrorEvent-listener to the loaders, so you can trace a bit more information for yourself. The code beneath is a general starter, it'll probably need a bit more information before it actually works in flash/flex.
swfLoaderInstance:SWFLoader = new SWFLoader();
swfLoaderInstance.source = "someSWFFile.swf";
swfLoaderInstance.addEventListener(IOErrorEvent.IO_ERROR, handleError);
public function handleError(event:IOErrorEvent):void{
trace(event.target);
//etc...
}
The problem was with mis-locating the SWF modules. As soon as appropriate location was set for generated SWF modules - the error disappear.
If it's a internet browser thing, and you are using Google Chrome. Go to Histor>Clear all browsing Data. Tick in these thins only, you wouldn't want to lose the browsing data.
Empty the cache,
Delete cookies and other site and plug-in data,
Clear saved Autofill form data
Clear it from beginning of time. Then try to load the thing you want to. Worked for me fine:)
I had the same error message. In my case, it was due to the Loader getting garbage collected.
This is the code I had issues with:
private function loadImageFromUrl( imageUrl:String ):AbstractOperation
{
var result:AbstractOperation = new AbstractOperation();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, function ( e:Event ):void
{
result.dispatchCompleteEvent( loader.content );
} );
loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, function ( event:IOErrorEvent ):void
{
result.dispatchErrorEvent( event );
} );
loader.load( new URLRequest( imageUrl ) );
return result;
}
And this is the good code:
private var m_loaderReferences:Dictionary = new Dictionary();
private function loadImageFromUrl( imageUrl:String ):AbstractOperation
{
var result:AbstractOperation = new AbstractOperation();
var loader:Loader = new Loader();
m_loaderReferences[imageUrl] = loader; // Need to keep a reference to the loader to avoid Garbage Collection
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, function ( e:Event ):void
{
result.dispatchCompleteEvent( loader.content );
delete m_loaderReferences[imageUrl];
} );
loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, function ( event:IOErrorEvent ):void
{
result.dispatchErrorEvent( event );
delete m_loaderReferences[imageUrl];
} );
loader.load( new URLRequest( imageUrl ) );
return result;
}
I reference the loader from a Dictionary to avoid the GC. I remove the loader from the Dictionary when it is done loading.

How can I test a SWF URL before Loading Styles From if (or catching the error)?

I am trying to use the following code to load styles from an external SWF, but I keep getting an ActionScript error when the URL is invalid:
Error: Unable to load style(Error #2036: Load Never Completed. URL: http://localhost/css/styles.swf): ../css/styles.swf.
at <anonymous>()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\styles\StyleManagerImpl.as:858]
private function loadStyles(): void
{
try
{
var styleEvent:IEventDispatcher =
StyleManager.loadStyleDeclarations("../styles.swf");
styleEvent.addEventListener(StyleEvent.COMPLETE,
loadStyle_completeHandler);
styleEvent.addEventListener(StyleEvent.ERROR,
loadStyle_errorHandler);
}
catch (error:Error)
{
useDefault();
}
}
private function loadStyle_completeHandler(event:StyleEvent): void
{
IEventDispatcher(event.currentTarget).removeEventListener(
event.type, loadStyle_completeHandler);
goToNextStep();
}
private function loadStyle_errorHandler(event:StyleEvent): void
{
IEventDispatcher(event.currentTarget).removeEventListener(
event.type, loadStyle_errorHandler);
useDefault();
}
I basically want to go ahead an use the default styles w/o the user seeing the error if this file can't be loaded - but I can't seem to find any way to do this.
Interesting problem. Try removing the removeEventListener call, or commenting it out; in my brief tests it appeared the event handler was being called twice (I'm not immediately sure why, although I suspect it has to do with style inheritance), and commenting that line did the trick.
If you have the same result, you might try just checking for the listener (using hasEventListener) first, before attaching it in your loadStyles() function, instead. Hope it helps!
** Not an answer, but an update:
FYI, this is the ActionScript code in the Source of mx.styles.StyleManagerImpl that is run when you call StyleManager.loadStyleDeclarations(). I ran the debugger and added a breakpoint at the Line 858("throw new Error(errorText);"), and the breakpoint was caught. I'm thinking it shouldn't be caught there, but the previous IF ("if (styleEventDispatcher.willTrigger(StyleEvent.ERROR))") should be run instead.
public function loadStyleDeclarations2(
url:String, update:Boolean = true,
applicationDomain:ApplicationDomain = null,
securityDomain:SecurityDomain = null):
IEventDispatcher
{
var module:IModuleInfo = ModuleManager.getModule(url);
var readyHandler:Function = function(moduleEvent:ModuleEvent):void
{
var styleModule:IStyleModule =
IStyleModule(moduleEvent.module.factory.create());
styleModules[moduleEvent.module.url].styleModule = styleModule;
if (update)
styleDeclarationsChanged();
};
module.addEventListener(ModuleEvent.READY, readyHandler,
false, 0, true);
var styleEventDispatcher:StyleEventDispatcher =
new StyleEventDispatcher(module);
var errorHandler:Function = function(moduleEvent:ModuleEvent):void
{
var errorText:String = resourceManager.getString(
"styles", "unableToLoad", [ moduleEvent.errorText, url ]);
if (styleEventDispatcher.willTrigger(StyleEvent.ERROR))
{
var styleEvent:StyleEvent = new StyleEvent(
StyleEvent.ERROR, moduleEvent.bubbles, moduleEvent.cancelable);
styleEvent.bytesLoaded = 0;
styleEvent.bytesTotal = 0;
styleEvent.errorText = errorText;
styleEventDispatcher.dispatchEvent(styleEvent);
}
else
{
throw new Error(errorText);
}
};
module.addEventListener(ModuleEvent.ERROR, errorHandler,
false, 0, true);
styleModules[url] =
new StyleModuleInfo(module, readyHandler, errorHandler);
// This Timer gives the loadStyleDeclarations() caller a chance
// to add event listeners to the return value, before the module
// is loaded.
var timer:Timer = new Timer(0);
var timerHandler:Function = function(event:TimerEvent):void
{
timer.removeEventListener(TimerEvent.TIMER, timerHandler);
timer.stop();
module.load(applicationDomain, securityDomain);
};
timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
timer.start();
return styleEventDispatcher;
}
I debugged the source, and found that the ERROR Event is being triggered twice. So, I simply set a flag the first time the ERROR event handler is triggered, and check that flag for a value of true before continuing:
private var isErrorTriggered:Boolean; // Default is false
private function loadStyle_completeHandler(event:StyleEvent):void
{
IEventDispatcher(event.currentTarget).removeEventListener(
event.type, loadStyle_completeHandler);
goToNextStep();
}
private function loadStyle_errorHandler(event:StyleEvent):void
{
if (isErrorTriggered)
return;
isErrorTriggered = true;
useDefault();
}

Resources