Hello
I am trying to create a "drop-in" extension for mx:image that supports
an URLRequest for source property. The reason is that the (server)
images should be retrieved using a POST and some custom headers added to
the HTTP request.
I have something that somewhat works but seems far from optimal, and
also behaves weird when I use the extended image control in an
itemrenderer
This is the code. I am sure this is not the way, but what is the way?
E.g. the listener stuff is wrong and setting the source triggers a
second load, which does not feel correct.
Seems like having image control with a URLRequest for its source is a
fairly common requirement
Any guidance would be really welcomed
Thanks,
Peter
The code
public class ImageFromURLRequest extends Image {
public function ImageFromURLRequest() {
super();
}
private var _listeners:ArrayCollection = new ArrayCollection();
private var stream:String = "content";
private var partIx:int = 0;
override public function
addEventListener(type:String,listener:Function,useCapture:Boolean =
false,priority:int = 0,useWeakReference:Boolean = false):void {
var lstnr:Object = new Object;
lstnr["type"] = type;
lstnr["listener"] = listener;
lstnr["useCapture"] = useCapture;
lstnr["priority"] = priority;
lstnr["useWeakReference"] = useWeakReference;
_listeners.addItem(lstnr);
trace("Listening for " +type);
}
override public function load(url:Object = null):void {
if(url is URLRequest) {
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
var imageLoaded:Function = function(event:Event):void {
source=urlLoader.data
if(hasEventListener(Event.COMPLETE)) {
dispatchEvent(event);
}
}
urlLoader.addEventListener(Event.COMPLETE,imageLoaded);
for each(var lstnr:Object in _listeners) {
urlLoader.addEventListener(lstnr["type"],lstnr["listener"],lstnr["useCap\
ture"],lstnr["priority"],lstnr["useWeakReference"]);
}
urlLoader.load(url as URLRequest);
} else {
super.load(url);
}
}
}