ByteArray生成图片

直接来代码:

package
{
	import com.adobe.images.PNGEncoder;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	/**
	 * ...
	 * @author aisajiajiao
	 * 
	 */	
	public class DynamicImageCreater extends Sprite
	{
		[Embed(source="image.png")]
		private var Image:Class;
		
		private var streams:ByteArray;
		private var fileReference:FileReference;
		private var bmd:BitmapData;
		
		public function DynamicImageCreater()
		{
			//这张图在舞台上只是展示用,表示舞台上有东西
			var img:Bitmap = Bitmap(new Image());
			addChild(img);
			
			bmd = new BitmapData(stage.stageWidth,stage.stageHeight,true,0);
			bmd.draw(this);
			
			//PNGEncoder是静态类,将BitmapData数据转换为ByteArray并返回
			streams = PNGEncoder.encode(bmd);
			
			fileReference = new FileReference();
			stage.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		private function clickHandler(e:MouseEvent):void
		{
			fileReference.save(streams,"Hello.png");
		}
	}
}

注意PNGEncoder这个类导自Adobe官方开源的AS3库as3corelib,其GitHub地址如下:https://github.com/mikechambers/as3corelib 

你可能感兴趣的:(function,image,Class,import,Adobe)