从URL加载png保存并显示它

时间:2012-01-10 04:43:07

标签: actionscript-3 flex flash-builder flex3 flex4.5

我是Flash Builder 4.6的新手,并尝试制作移动应用程序。 我正在尝试从网站加载一个png文件,然后使用PersistenceManager保存它以供以后显示。但没有任何表现。 这是我加载图像的方式,看起来它正在工作。

    public class MySetup
{
    public var myId:String;
    public var myName:String;
    public var myImage:Object;
}
   protected function loadImage():void{
    var request:URLRequest = new URLRequest ("http://www.myweb.com/pictures/myImage.png");
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.BINARY;
    loader.load(request);
    loader.addEventListener(Event.COMPLETE, oncomplete);
    }

    protected function oncomplete(e:Event):void{ 
var saveManager:PersistenceManager = new PersistenceManager();
var loader2:URLLoader = e.target as URLLoader; 
if (loader2 != null){ 
        mySetup.myLogo = loader2.data;

        } 
        else{ 
            trace("an error has occured!"); 
        } 
        mySetup.myId = myId.text;
        mySetup.myName = myName.text;
        saveManager.setProperty("setupData", mySetup);
        saveManager.save();

        } 

现在我尝试显示图像,然后出现错误:

    private var myLogo:Image;
    protected function view1_creationCompleteHandler(event:FlexEvent):void
    {
    var loadManager:PersistenceManager = new PersistenceManager();
    if(loadManager.load())
        {
        var savedData:Object = loadManager.getProperty("setupData");    
            if(savedData){
                        myLogo = savedData.myLogo;
                        }
         }
     }


     <s:Image id="MyImage" y="10" width="290" height="164" scaleMode="stretch" source="myLogo"/>

感谢您的帮助。 金

1 个答案:

答案 0 :(得分:1)

myLogo必须是ByteArray。