Reputation: 183
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
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.
Matrix.identity()
before drawing. This will be of use if you perform this operation many times during one application session.img()
).Upvotes: 1