0

I'm trying to load PNG images with ActionScript with a Loader object. This works fine for some of the images (the INIT and COMPLETE events are fired as expected), for some other it doesn't. I've read in this thread that a URLLoader might help, so I tried that, using the loadBytes() function afterwards. Still doesn't work: the URLLoader fires the COMPLETE event, but the LoaderInfo object does not.

I've written a sample class that demonstrates the problem with two files (one working, the other one not).

public class LoaderTest extends MovieClip {
    var output:TextField;
    var loader:Loader;
    var urlLoader:URLLoader;

    function LoaderTest() {
        output = new TextField();
        output.width = 1000;
        output.height = 1000;
        output.multiline = true;
        addChild(output);

        var t1:Timer = new Timer(0, 0);
        t1.addEventListener(TimerEvent.TIMER, function() {
            t1.stop(); loadMapDirect("map_in_big.png");
        });

        var t2:Timer = new Timer(1000, 0);
        t2.addEventListener(TimerEvent.TIMER, function() {
            t2.stop(); loadMapDirect("map_us_big.png");
        });

        var t3:Timer = new Timer(2000, 0);
        t3.addEventListener(TimerEvent.TIMER, function() {
            t3.stop(); loadMapBytes("map_in_big.png");
        });

        var t4:Timer = new Timer(3000, 0);
        t4.addEventListener(TimerEvent.TIMER, function() {
            t4.stop(); loadMapBytes("map_us_big.png");
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }

    function loadMapBytes(url:String):void {
        try {
            urlLoader = new URLLoader();
            urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            urlLoader.addEventListener(ProgressEvent.PROGRESS, progressListener);
            urlLoader.addEventListener(Event.COMPLETE, completeListenerBytes);

            output.appendText("\nLoading '"+url+"' with URLLoader ");
            urlLoader.load(new URLRequest(url));
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }

    }

    function completeListenerBytes(e:Event):void {
        output.appendText("COMPLETE Event fired for URLLoader!\n");

        try {
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);
            output.appendText("Loading bytes with Loader ");
            loader.loadBytes(e.target.data as ByteArray);
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }
    }

    function loadMapDirect(url:String):void {
        try {
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);

            output.appendText("\nLoading '"+url+"' with Loader ");
            loader.load(new URLRequest(url));
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }
    }

    function completeListenerDirect(e:Event):void {
        var bmd:BitmapData = Bitmap(e.target.loader.content).bitmapData;
        output.appendText("COMPLETE Event fired for Loader! => h: " +  bmd.height + ", w: " + bmd.width + "\n");
    }

    function progressListener (e:ProgressEvent):void{
        output.appendText(".");
        if (e.bytesLoaded == e.bytesTotal) {
            output.appendText(" progress complete, " + e.bytesTotal + " bytes loaded!\n");
        }
    }
}

All images were generated with the PHP GD library and I'm compiling with SWFTools's as3compile.

You can see the script it in action on http://www.wichte-sind-wichtig.de/as3loader/loaderTest.swf

The two images map_in_big.png and map_us_big.png are in the same folder (not allowed to post more hyperlinks).

Any ideas?

Community
  • 1
  • 1
JVo
  • 3
  • 4

1 Answers1

1

The problem is that your application is probably compiled for Flash Player 9. In version 9 the maximum allowed image dimensions are 2880 x 2800 and map_us_big.png is 3150 x 1570. I ran the application successfully when I compiled it for Flash Player 10.

Here's a reference http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#BitmapData%28%29

In AIR 1.5 and Flash Player 10, the maximum size for a BitmapData object is 8,191 pixels in width or height, and the total number of pixels cannot exceed 16,777,215 pixels. (So, if a BitmapData object is 8,191 pixels wide, it can only be 2,048 pixels high.) In Flash Player 9 and earlier and AIR 1.1 and earlier, the limitation is 2,880 pixels in height and 2,880 pixels in width. If you specify a width or height value that is greater than 2880, a new instance is not created.

Fearnor
  • 121
  • 3