Flex 学习笔记 flexlib控件之_Base64Image(图片Base64互换
开始学习flexlib控件咯!
图片和Base64如何转换,可以使用flexlib:Base64Image控件实现图片转Base64,ImageSnapshot实现Base64转图片。
下面实现方法不一定是最好的,如有更好的方法望大家指点。谢谢!
View Code
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:flexlib="http://code.google.com/p/flexlib/" >
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.utils.ByteArray;
import mx.utils.Base64Decoder;
import mx.utils.StringUtil;
import mx.graphics.ImageSnapshot;
[Embed(source="assets/images/aaa.gif")]
public var cat:Class;
protected function btnSetPic_clickHandler(event:MouseEvent):void
{
lib64Img2.source=cat;
}
protected function ImgToBase64_clickHandler(event:MouseEvent):void
{
var ohSnap:ImageSnapshot = ImageSnapshot.captureImage(lib64Img2);
txta1.text = ImageSnapshot.encodeImageAsBase64(ohSnap);
}
protected function Base64ToImg_clickHandler(event:MouseEvent):void
{
lib64Img.value=txta2.text;
}
]]>
</fx:Script>
<s:HGroup>
<s:Button y="120" label="设置图片" id="btnSetPic" click="btnSetPic_clickHandler(event)"/>
<flexlib:Base64Image id="lib64Img2" width="100" height="100" />
<s:Button id="Base64ToImg" label="图片转Base64" click="ImgToBase64_clickHandler(event)" />
<s:TextArea id="txta1" width="200" height="100" />
</s:HGroup>
<s:HGroup y="150">
<s:TextArea id="txta2" width="200" height="100" />
<s:Button id="ImgToBase64" label="Base64转图片" click="Base64ToImg_clickHandler(event)" />
<flexlib:Base64Image id="lib64Img" width="100" height="100"/>
</s:HGroup>
</s:Application>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:flexlib="http://code.google.com/p/flexlib/" >
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.utils.ByteArray;
import mx.utils.Base64Decoder;
import mx.utils.StringUtil;
import mx.graphics.ImageSnapshot;
[Embed(source="assets/images/aaa.gif")]
public var cat:Class;
protected function btnSetPic_clickHandler(event:MouseEvent):void
{
lib64Img2.source=cat;
}
protected function ImgToBase64_clickHandler(event:MouseEvent):void
{
var ohSnap:ImageSnapshot = ImageSnapshot.captureImage(lib64Img2);
txta1.text = ImageSnapshot.encodeImageAsBase64(ohSnap);
}
protected function Base64ToImg_clickHandler(event:MouseEvent):void
{
lib64Img.value=txta2.text;
}
]]>
</fx:Script>
<s:HGroup>
<s:Button y="120" label="设置图片" id="btnSetPic" click="btnSetPic_clickHandler(event)"/>
<flexlib:Base64Image id="lib64Img2" width="100" height="100" />
<s:Button id="Base64ToImg" label="图片转Base64" click="ImgToBase64_clickHandler(event)" />
<s:TextArea id="txta1" width="200" height="100" />
</s:HGroup>
<s:HGroup y="150">
<s:TextArea id="txta2" width="200" height="100" />
<s:Button id="ImgToBase64" label="Base64转图片" click="Base64ToImg_clickHandler(event)" />
<flexlib:Base64Image id="lib64Img" width="100" height="100"/>
</s:HGroup>
</s:Application>