forum > Use haxe.Http for images?
-
James Aug 14 at 02:50
Hi, I'm very new to Haxe and am trying some simple tests to become more familiar. At this point my aim is to use as much of the haxe API as possible (and as little of the platform dependent APIs). To experience this in action I'm doing some simple experiments loading images into a project that I can export to JS and flash. I've already got a bit of conditional compiling going on so I know it's enevitable that there will be some slight differences in the code between the platforms.
The question I have at the moment is if haxe.Http can be used in a Flash context to load images from an external server. The code below seems to me like it should work, but I'm getting an "Error #2124: Loaded file is an unknown type." error when trying to use Loader.loadBytes. (The code below doesn't show any of the JavaScript stuff).
function loadImage{ var myImageRequest = new Http(SERVER_IMAGE_PATH); myImageRequest.onData = doImageLoadedAS; myImageRequest.request(false); } function doImageLoadedAS(thisResult) { var myLoader = new Loader(); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, doBytesLoaded); myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, doBytesError); var myByteData:ByteArray = Bytes.ofString(thisResult).getData(); myLoader.loadBytes(myByteData); } function doBytesError(e) { trace(e.text); // This error is triggered } function doBytesLoaded(e) { var myContent = e.currentTarget.content; var myBitmapData = new BitmapData(myContent.width, myContent.height ); myBitmapData.draw(myContent, new Matrix()); var myBitmap:Bitmap = new Bitmap(myBitmapData); Lib.current.addChild(myBitmap); }
Should this work? Or am I on a wild goose chase?
-
James Aug 14 at 03:54
Here's the complete code. Just a note, the "doBytesLoaded" function never gets called as the error thrown by loadBytes prevents it. I mention this because whatever issue is happening is upstream from this function - however if there is something wrong with this function of course Ill need to fix that too eventually. The focus right now is to determine why that error is being thrown.
package ; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.IBitmapDrawable; import flash.display.Loader; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.UncaughtErrorEvent; import flash.geom.Matrix; import flash.Lib; import flash.utils.ByteArray; import haxe.Http; import haxe.io.Bytes; class Main { inline static var IMAGE_FILE:String = "http://www.adobe.com/images/cs4/noflash_656X420.jpg"; static function main() { new Main(); } public function new () { var myImageRequest = new Http(IMAGE_FILE); myImageRequest.onData = doImageLoadedAS; myImageRequest.request(false); /**/ } function doImageLoadedAS(thisResult) { var myLoader = new Loader(); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, doBytesLoaded); myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, doBytesError); var myByteData:ByteArray = Bytes.ofString(thisResult).getData(); myLoader.loadBytes(myByteData); } function doBytesError(e) { // This error triggers: Error #2124: Loaded file is an unknown type. trace(e.text); } function doBytesLoaded(e) { // This block never gets called as the loadBytes call triggeres the above error. var myContent = e.currentTarget.content; var myBitmapData = new BitmapData(myContent.width, myContent.height ); myBitmapData.draw(cast (myContent,IBitmapDrawable), new Matrix()); var myBitmap:Bitmap = new Bitmap(myBitmapData); Lib.current.addChild(myBitmap); } }
A final note to mention is if I try to use the debugger (by setting break points) I get a .Net errror window appear.
-
James Aug 14 at 04:57
FYI, here's the same thing in pure AS3 which works (from the IDE), confirming that the image/server are accessible:
import flash.net.URLRequest; import flash.display.Loader; import flash.events.Event; import flash.events.IOErrorEvent; // const IMAGE_FILE:String = "http://www.adobe.com/images/cs4/noflash_656X420.jpg"; var myRequest:URLRequest = new URLRequest(IMAGE_FILE); var myLoader:Loader = new Loader(); myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, doError); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, doSuccess); myLoader.load(myRequest); // function doError(e:IOErrorEvent):void { trace(e.text); } // function doSuccess(e:Event):void { var myBitmap:Bitmap = e.target.content as Bitmap; addChild(myBitmap); }
-
James Aug 14 at 07:05
This issue: https://code.google.com/p/haxe/issues/detail?id=1144 has been added in relation to this but it seems that it's arguably the expected functionality due to the fact that URLLoader is set to transfer as string and not binary in the Http.hx file. It seems like Bytes.ofString() isn't totally reliable in creating the ByteArray for Flash.
FYI, this code works using URLLoader (on a confirmed domain with an open crossdomain policy). Without setting the URLLoaderDataFormat to BINARY it won't work.
import flash.net.URLRequest; import flash.net.URLLoader; import flash.events.Event; import flash.events.IOErrorEvent; Security.allowDomain("*"); //; const IMAGE_FILE:String = "http://adotube.com/site/sites/all/themes/adotube/images/logo.png"; var myRequest:URLRequest = new URLRequest(IMAGE_FILE); var myLoader:URLLoader = new URLLoader(); myLoader.dataFormat = URLLoaderDataFormat.BINARY myLoader.addEventListener(IOErrorEvent.IO_ERROR, doError); myLoader.addEventListener(Event.COMPLETE, doSuccess); myLoader.load(myRequest); //; function doError(e:IOErrorEvent):void{ trace(e.text); } function doSuccess(e:Event):void{ var loader:URLLoader = URLLoader(e.target); var myBitmap:Bitmap = loader.data as Bitmap; var myLoader = new Loader(); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, doBytesLoaded); myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, doBytesError); myLoader.loadBytes(loader.data); } function doBytesError(e){ trace(e.text); } function doBytesLoaded(e){ var myContent = e.currentTarget.content; var myBitmapData:BitmapData = new BitmapData(myContent.width,myContent.height); myBitmapData.draw(myContent, new Matrix()); var myBitmap:Bitmap = new Bitmap(myBitmapData); addChild(myBitmap); }
-
Massive have released a loader, mloader solution, it requires you use mSignal. Also you could look at using NME, but I think it is probably better to use a separate wrapper class for loading in as3 and js haxe but maybe have the same interface so use the --remap to swap the classes with the target on compile.
-
James Aug 15 at 02:33
Looks like a solid extension to the haxe.Http functionality, amkes things a bit easier :)