Atm
Atm

Reputation: 183

what's the most resources efficient way to take a screenshot of display object in as3

What's the most resources efficient way to take a screenshot of display object in as3?

This is the code I am currently using:

public static function img(o:DisplayObject,width:int,height:int):ByteArray
    {
        var b:BitmapData = new BitmapData(width,height,true,0x000000);
        b.draw(o,new Matrix(o.width/width,0,0,o.height/height),null,null,null,true);
        return new JPGEncoder(35).encode(b);
    }

But it takes too much CPU power. I am okay if it will be processed more slowly, but without CPU utilization up to 60%.

Thanks.

Upvotes: 1

Views: 285

Answers (1)

Michael Antipin
Michael Antipin

Reputation: 3532

It's JPEG encoding that takes most of the time, and not capturing of a displayobject to a BitmapData.

To achieve better performance (sacrificing its running time) you have to use some optimized version of standard JPEGEncoder class or/and its asynchronous version.

If you are not satisfied with the above, try googling for similar solutions: some guys out there have already solved the problem.

Note: you can also implement a couple of optimizations.

  • You don't need to create new Matrix instance every time. You can use one instance, calling Matrix.identity() before drawing. This will be of use if you perform this operation many times during one application session.
  • You don't need to create new JPEGEncoder instance every time. You can create one and hold it in some private static field (for example, create it on first call to img()).

Upvotes: 1

Related Questions