问题
For the past few hours I have been trying to clone an image in Flex (using the Spark Components, but also trying to convert between Bitmap and BitmapImage).
What I am trying exactly is to create a simple painting application which keeps track of each Brush-Stroke. As soon as the Image on the Canvas has changed, it is to be cloned and then the clone is to be put into the History-Panel on the bottom of the application.
Things I have tried include:
- Using ObjectUtils.clone(Object)
- Creating BitmapData from Image.content, then making it a Bitmap and simply display it (Image doesn't have a content field, it says)
- Performing a byte-copy and others I could find on the internet, of course.
So basically, how does one clone an Image (Spark Image) in Flex 4.6?
Thank you very much!
-- Danny Nophut
回答1:
Instead of cloning you can get the image of the drawing and set the bitmap of the image as source to the history image, do some thing like this
private function getBitmapData( target:DisplayObject ) : BitmapData
{
//target.width and target.height can also be replaced with a fixed number.
var bd : BitmapData = new BitmapData( target.width, target.height );
bd.draw( target );
return bd;
}
In some case if the width and height of the target is not working you can use the getbounds method to get the bounds of the object and from the bounds take the width and height.
回答2:
There's a clone function on a bitmapdata:
public class EZB2ParkObject extends Image implements IEZB2ParkObject
{
public function clone():IEZB2ParkObject{
var n:IEZB2ParkObject = new EZB2ParkObject();
n.id = this.id;
n.source = new Bitmap(BitmapData(this.source.bitmapData).clone());
n.dimensions = this.dimensions;
n.assetId = this.assetId;
return n;
}
}
来源:https://stackoverflow.com/questions/9670534/clone-an-image-in-flex-4-6