加载PNG图片的处理方式

现在有很多加载在线图片的文章,这里小弟只是随手拈来,并提上两手
首先,用解码的方式来加载在线图片并生成image,这里有用到PNGDecoder,可以屋GooleCode里查找,下载。不过老实说,PNGDecoder这个解码是很差的,其中ALPHA的解码尤为糟糕,第一种就是利用它来加载后解码而出现勒类似马赛克的彩色效果

 1 <?xml version="1.0"?>
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
3 <mx:Script>
4 <![CDATA[
5
6 import mx.core.UIComponent;
7 import mx.core.BitmapAsset;
8 import flash.net.*;
9 import flash.display.*;
10 import flash.utils.ByteArray;
11 //导入BMPDecoder
12 import com.adobe.image.PNGDecoder;
13 private function showimage():void {
14 loadPNGFile("../test.png");
15 }
16 private function loadPNGFile( url:String ):void {
17 var loader:URLLoader = new URLLoader();
18 //指定loader以二进制返回数据
19 loader.dataFormat = URLLoaderDataFormat.BINARY;
20 loader.addEventListener( Event.COMPLETE, onCompleteLoad );
21 loader.load( new URLRequest( url ) );
22 }
23 private function onCompleteLoad( e:Event ):void {
24 var byte:ByteArray = e.target as ByteArray;
25 _loader=new Loader();
26 _loader.addEventListener(Event.COMPLETE,comHandler);
27 _loader.loadbytes(byte);
28 }
29 private function comHandler(evt:Event):void{
30 var loader:URLLoader = e.target as URLLoader;
31
32 var decoder:PNGDecoder = new PNGDecoder();
33
34 //将二进制数据转换成BitmapData
35
36 var bd:BitmapData = decoder.decode( loader.data );
37 //这时候能获取到loader.data为ByteArray,
38 //若不加URLLoaderDataFormat.BINARY,
39 //则获取到的肯能为TEXT类型
40
41 //用来显示效果的Bitmap获取位图数据,图片平滑
42
43 var myBitmap:Bitmap = new Bitmap(bd,"auto",true);
44 myImage.source=myBitmap;
45 }
46 ]]>
47 </mx:Script>
48
49 <mx:Button
50 id="showImage"
51 label="showimage"
52 click="showimage();"/>
53
54 <mx:Image id="myImage" />
55 </mx:Application>


通过URLLoader加载后再用Loder加载,然后获取ByteArray,然后再进行BitmapData的转换,
最后,也可以通过Draw(),进行初始化

[code]<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[

import mx.core.UIComponent;
import mx.core.BitmapAsset;
import flash.net.*;
import flash.display.*;
import flash.utils.ByteArray;
//导入BMPDecoder
import com.adobe.image.PNGDecoder;
private function showimage():void {
loadPNGFile("../test.png");
}
private function loadPNGFile( url:String ):void {
var loader:URLLoader = new URLLoader();
//指定loader以二进制返回数据
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener( Event.COMPLETE, onCompleteLoad );
loader.load( new URLRequest( url ) );
}
private function onCompleteLoad( e:Event ):void {
var byte:ByteArray = e.target as ByteArray;
_loader=new Loader();
_loader.addEventListener(Event.COMPLETE,comHandler);
_loader.loadbytes(byte);
}
private var _loader:Loader;
private function comHandler(evt:Event):void{
_loader= new Loader();
var byte:ByteArray=evt.target as ByteArray;
_loader.contentLoderInfo.addEventListener(evt.COMPLETE,comHandler);
_loader.loadBytes(byte);
}
private function comHandler(evt:Event):void{
//将二进制数据转换成BitmapData

var bitmapdata:BitmapData = new BitmapData(480,320,true,0);
bitmapdata.draw(_loader,new Matrix(),null,null,null,true);
//用来显示效果的Bitmap获取位图数据,图片平滑
//其实也可以直接省略Draw这部的
var myBitmap:Bitmap = new Bitmap(bitmapdata,"auto",true);
myImage.source=myBitmap;
//或者直接myImage.addChild(myBitmap)
}
]]>
</mx:Script>

<mx:Button
id="showImage"
label="showimage"
click="showimage();"/>

其实两种实现方法的对比就会有答案了

<mx:Image id="myImage" />
</mx:Application>


 

posted on 2011-11-14 14:25  AS3.0  阅读(1538)  评论(0编辑  收藏  举报

导航