<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="app_init()">
<mx:Script>
<![CDATA[
import flash.utils.setTimeout;
import mx.controls.Alert;
import mx.controls.Image;
import mx.graphics.codec.IImageEncoder;
import mx.graphics.codec.JPEGEncoder;
import mx.graphics.codec.PNGEncoder;
[Bindable]private var imgUrl:String = "assets/map.jpg";//素材路径
[Bindable]private var MAP_W:int = 0;
[Bindable]private var MAP_H:int = 0;
//切图规格
private const GW:int = 200;
private const GH:int = 200;
/**
* 地图切图程序
* @desc: 用于提供一张原始位图,切成若干小图片
* @author: weixiao
* @data: 2012-7-15
* */
protected function app_init():void
{
log.htmlText = "正在生成地图......";
//下载大地图
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderCompile);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIoError);
loader.load(new URLRequest(imgUrl));
}
/**
* @return 地图下载完成,切图
* */
private function onLoaderCompile(e:Event):void{
e.target.removeEventListener(Event.COMPLETE,onLoaderCompile);
var source:BitmapData = e.target.content.bitmapData;
MAP_W = source.width;
MAP_H = source.height;
//需要切的行和列
var cols:int = Math.ceil(source.width/GH);
var rows:int = Math.ceil(source.height/GW);
//切图
var copySmall:BitmapData = null;
for(var i:int = 0; i<rows; i++){
for(var j:int = 0; j<cols; j++){
copySmall = new BitmapData(GW,GH,true,0);
copySmall.copyPixels(source,new Rectangle(j*GW,i*GH,GW,GH),new Point(0,0));
var bmp:Bitmap = new Bitmap(copySmall);
var _encoder:IImageEncoder=new PNGEncoder();//用于编码
var ba:ByteArray=_encoder.encode(copySmall);//编码成JPG图片,质量为80
var airf:File = new File(File.applicationStorageDirectory.url+"source/"+i+"_"+j+".png");
var fs:FileStream = new FileStream();
fs.openAsync(airf, FileMode.WRITE);
fs.writeBytes(ba);
log.htmlText = log.htmlText+"\n"+airf.nativePath;
fs.close();
}
}
}
/**
* @return 地图路径错误
* */
private function onIoError(e:Event):void{
Alert.show("素材路径错误: "+imgUrl);
e.target.removeEventListener(Event.COMPLETE,onLoaderCompile);
}
]]>
</mx:Script>
<mx:VBox>
<mx:Text id="log0" width="230" text="地图大小: {MAP_W}×{MAP_H}"/>
<mx:Text id="log2" width="230" text="切图规格: {GW}×{GH}"/>
<mx:Text id="log"/>
</mx:VBox>
</mx:WindowedApplication >
<mx:WindowedApplication xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="app_init()">
<mx:Script>
<![CDATA[
import flash.utils.setTimeout;
import mx.controls.Alert;
import mx.controls.Image;
import mx.graphics.codec.IImageEncoder;
import mx.graphics.codec.JPEGEncoder;
import mx.graphics.codec.PNGEncoder;
[Bindable]private var imgUrl:String = "assets/map.jpg";//素材路径
[Bindable]private var MAP_W:int = 0;
[Bindable]private var MAP_H:int = 0;
//切图规格
private const GW:int = 200;
private const GH:int = 200;
/**
* 地图切图程序
* @desc: 用于提供一张原始位图,切成若干小图片
* @author: weixiao
* @data: 2012-7-15
* */
protected function app_init():void
{
log.htmlText = "正在生成地图......";
//下载大地图
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderCompile);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIoError);
loader.load(new URLRequest(imgUrl));
}
/**
* @return 地图下载完成,切图
* */
private function onLoaderCompile(e:Event):void{
e.target.removeEventListener(Event.COMPLETE,onLoaderCompile);
var source:BitmapData = e.target.content.bitmapData;
MAP_W = source.width;
MAP_H = source.height;
//需要切的行和列
var cols:int = Math.ceil(source.width/GH);
var rows:int = Math.ceil(source.height/GW);
//切图
var copySmall:BitmapData = null;
for(var i:int = 0; i<rows; i++){
for(var j:int = 0; j<cols; j++){
copySmall = new BitmapData(GW,GH,true,0);
copySmall.copyPixels(source,new Rectangle(j*GW,i*GH,GW,GH),new Point(0,0));
var bmp:Bitmap = new Bitmap(copySmall);
var _encoder:IImageEncoder=new PNGEncoder();//用于编码
var ba:ByteArray=_encoder.encode(copySmall);//编码成JPG图片,质量为80
var airf:File = new File(File.applicationStorageDirectory.url+"source/"+i+"_"+j+".png");
var fs:FileStream = new FileStream();
fs.openAsync(airf, FileMode.WRITE);
fs.writeBytes(ba);
log.htmlText = log.htmlText+"\n"+airf.nativePath;
fs.close();
}
}
}
/**
* @return 地图路径错误
* */
private function onIoError(e:Event):void{
Alert.show("素材路径错误: "+imgUrl);
e.target.removeEventListener(Event.COMPLETE,onLoaderCompile);
}
]]>
</mx:Script>
<mx:VBox>
<mx:Text id="log0" width="230" text="地图大小: {MAP_W}×{MAP_H}"/>
<mx:Text id="log2" width="230" text="切图规格: {GW}×{GH}"/>
<mx:Text id="log"/>
</mx:VBox>
</mx:WindowedApplication >