啊,對不起,我以爲你只是想加載圖像;我沒有看到你在試圖挽救它。
對於以下兩種情況下,你需要將圖像加載爲二進制:
var urlLoader:URLLoader = new URLLoader(new URLRequest(myURL));
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
...
這是因爲,如果我們正常加載它(使用Loader
),然後就是我們得到的是一個Bitmap
對象,即Flash的圖像表示,而不是jpg/png數據本身。加載時使用這種方法會給你一個ByteArray
。
如果您使用AIR,你應該能夠使用FileStream
類:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html
是這樣的:如果你使用的Flash
// NOTE: you can use any of the other File constants to get your path (e.g.
// documentsDirectory, desktopDirectory...
// myImageFileName is the name of your image, e.g. "myPic.jpg"
var file:File = File.applicationStorageDirectory.resolvePath(myImageFileName);
var fs:FileStream = new FileStream;
try
{
fs.open(file, FileMode.WRITE);
fs.writeBytes(imageBinaryData); // imageBinaryData is a ByteArray
fs.close();
}
catch (e:Error)
{
trace("Can't save image: " + e.errorID + ": " + e.name + ": " + e.message);
}
,那麼唯一的辦法,你可以保存沒有用戶交互的東西是通過SharedObject
。這意味着你的數據將不會在應用程序外部提供(這將是一個.sol
文件),但取決於你如何做,這可能不成問題。
// get our shared object - if you're saving a lot of images, then you might need another shared
// object, whose name you know, that stores the names of the other images
var so:SharedObject = null;
try { so = SharedObject.getLocal(this.m_name); }
catch (e:Error)
{
trace("Couldn't get shared object: " + e.errorID + ": " + e.name + ": " + e.message);
return;
}
// NOTE: it's possible you may need a registerClassAlias on the ByteArray before this
so.data["image"] = imageBinaryData;
// save the lso
try { so.flush(minDiskSpace); }
catch (e:Error)
{
trace("Couldn't save the lso: " + e.errorID + ": " + e.name + ": " + e.message);
}
要真正在以後使用你的形象,加載文件(二進制模式)/讓您SharedObject
,並保存二進制轉換爲圖像:
var l:Loader = new Loader;
l.contentLoaderInfo.addEventListener(Event.COMPLETE, this._onConvertComplete); // you could probably also listen for the IO_ERROR event
try
{
// NOTE: you can pass a LoaderContext to the loadBytes methods
l.loadBytes(imageBinaryData);
}
catch(e:Error)
{
trace("Couldn't convert image: " + e.errorID + ": " + e.name + ": " + e.message);
}
...
// called when our loader has finished converting our image
private function _onConvertComplete(e:Event):void
{
// remove the event listeners
var loaderInfo:LoaderInfo = (e.target as LoaderInfo);
loaderInfo.removeEventListener(Event.COMPLETE, this._onConvertComplete);
// get our image
var bitmap:Bitmap = loaderInfo.content;
this.addChild(bitmap);
}
如果你不能使用任何的那些方法,那麼你將不得不進行某種用戶交互(例如鼠標點擊)。 (出於好奇,你有沒有試過在相關對象上發送一個MouseEvent.CLICK
,看看它是否會工作?)
你是如何加載你的圖像?如果你只是從XML獲得一個URL,那麼一個普通的'Loader'應該可以工作。鼠標點擊限制通常僅用於打開URL('navigateToURL') – divillysausages
一切正常,並且它也是一個加載程序,問題是隻能通過Btn單擊來激活!使用這個。點擊= 「loadRemoteImage(textboxLink.text)」。 – aktell
'textboxLink.text'只是一個帶有你的url的標籤。你如何加載它完全取決於你 - 你只是選擇加載一個'點擊'行動;你可以很容易地擁有一個'Timer','setInterval',甚至是一個鍵盤事件;當你知道標籤已經填充時,你只需要打電話給它 – divillysausages