Drawing bytearray image


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()

This is somehow related to the fact that the rectangle im setting is too large. However the image is 271x278, and also when I try to load the $imgData in a uiloader it works fine - the image is shown!

If I decrease the rectangle size to 20 x20 nothing is being drawn.
Any thoughts?

Thank you