Skip to main content
Known Participant
June 1, 2009
Answered

ByteArray to Sprite

  • June 1, 2009
  • 1 reply
  • 4208 views

Hi   I'm having problems with having my bytearray (which is an image) drawn into a sprite. This is my attempt so far:

var imgB:BitmapData = new BitmapData(271, 278, true, 0xff0000);

imgB.setPixels(new Rectangle(0,0,271,278), $imgData);

$image.graphics.beginBitmapFill(imgB,new Matrix(), false, true);

My byteArray is $imgData, and the sprite that I want to draw to is $image. When I run the above code I get EOF error:

Error: Error #2030: End of file was encountered.      at flash.display::BitmapData/setPixels()

Any thoughts?  Thank you

This topic has been closed for replies.
Correct answer Greg Dove

Also I discovered something new.

When I try to draw the byteArray onto the sprite, the result is messed up pixels. See the attachment. But the UILoader loads the same byte array fine. How can this be?


I'm not sure what you're trying to do here...but you cannot use the binary data you loaded with a URLLoader as bitmapData if it was loaded from a png or jpeg for example. Those file formats need to be decoded into bitmapData - using the player's native support for that seems best.

There does seem to be a frame delay with the loadBytes method and you can't catch it using the COMPLETE event, as you say.

I got it working like this (perhaps there is a better way than using the enterFrame, but I just did this quickly to check)

var $image:Sprite=new Sprite();

addChild($image);

var myBitmapData:BitmapData;
var imageLoader:Loader = new Loader()

var dataLoader:URLLoader=new URLLoader();
dataLoader.dataFormat = flash.net.URLLoaderDataFormat.BINARY

dataLoader.addEventListener(Event.COMPLETE,completeHandler)

function loadImage(url:String):void{
   
    var req:URLRequest=new URLRequest(url);
    dataLoader.load(req)

}


function completeHandler(e:Event):void{
    //use enterFrame to check for loaded content
    imageLoader.addEventListener(Event.ENTER_FRAME,imageDecodeHandler);
    imageLoader.loadBytes(e.currentTarget.data);

   
}
function imageDecodeHandler(e:Event):void{

    if (imageLoader.content) {
        trace('loaded')
        imageLoader.removeEventListener(Event.ENTER_FRAME,imageDecodeHandler);
        myBitmapData = Bitmap(imageLoader.content).bitmapData;

        //imageLoader.content width and height available now as well as myBitmapdata.rect etc

        $image.graphics.beginBitmapFill(myBitmapData,null,true,true);
        $image.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
        $image.graphics.endFill()

    } else {
        trace('not yet loaded')
    }
}

//substitute a local image file here:
loadImage("46.png")

1 reply

kglad
Community Expert
Community Expert
June 1, 2009

you don't have enough data in your bytearray.

(and the bytearray is not a display object so it won't have a graphics property.)

makportalAuthor
Known Participant
June 1, 2009

Thanks for the quick answer.

But if I use the byte array to load it into a UILoader component:

myloader.loadBytes($imgData);

The image loads fine.

I know that a bytearray is not a display object. The $image variable is a sprite.

Any other suggestions?

kglad
Community Expert
Community Expert
June 1, 2009

then your rectangle's too large.

(and my bad;  i misread $image as being the same as $imgData.)