如果將圖像嵌入到Flex應用程序中,圖像的加載速度會更快。 More info on that here。這背後的代碼應該是這樣的:如果你需要使用相同的圖像
<s:Image id="myImageRaw" source="{imgCls}"/>
[從文檔複製代碼]
:
[Embed(source="logo.gif")]
[Bindable]
public var imgCls:Class;
然後你可以使用這個類像這樣多次,你應該看看使用BitMapImage類;並克隆第一個圖像的bitMapData。這是一個快速實用工具類I「Borrowed and modified」,用於從精靈獲取BitMapData,反之亦然。 [Flex的Image類類擴展精靈,所以你應該能夠在發送圖像作爲輸入]
package com.natejc.utils.display
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Sprite;
// **********************************************************************************
// **********************************************************************************
// borrowed from http://www.natejc.com/source/com/natejc/utils/display/DisplayConverter.as
/**
* Provides convenience conversion methods for Sprites and Bitmaps.
*
* Open source. Free to use. Licensed under the MIT License.
*
* @author Nate Chatellier
* @see http://blog.natejc.com
*/
public class DisplayConverter
{
// **********************************************************************************
/**
* Constructs the DisplayConverter object.
*/
public function DisplayConverter()
{
trace("DisplayConverter is a static class and should not be instantiated");
} // END CONSTRUCTOR
// **********************************************************************************
/**
* Converts a Bitmap to a Sprite.
*
* @param bitmap The Bitmap that should be converted.
* @param smoothing Whether or not the bitmap is smoothed when scaled.
* @return The converted Sprite object.
*
* @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Bitmap.html#smoothing
*/
public static function bitmapToSprite(bitmap:Bitmap, smoothing:Boolean = false):Sprite
{
var sprite:Sprite = new Sprite();
sprite.addChild(new Bitmap(bitmap.bitmapData.clone(), "auto", smoothing));
return sprite;
} // END FUNCTION bitmapToSprite
// **********************************************************************************
/**
* Converts a Sprite to a Bitmap.
*
* @param sprite The Sprite that should be converted.
* @param smoothing Whether or not the bitmap is smoothed when scaled.
* @return The converted Bitmap object.
*
* @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#draw()
*/
public static function spriteToBitmap(sprite:Sprite, smoothing:Boolean = false):Bitmap
{
var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00FFFFFF);
bitmapData.draw(sprite);
return new Bitmap(bitmapData, "auto", smoothing);
} // END FUNCTION spriteToBitmap
/**
* JH DotComIT added 11/19/2011
* Converts a Sprite to a BitmapData.
*
* @param sprite The Sprite that should be converted.
* @return The converted Bitmap object.
*
* @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#draw()
*/
public static function spriteToBitmapData(sprite:Sprite):BitmapData
{
var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00FFFFFF);
bitmapData.draw(sprite);
return bitmapData;
} // END FUNCTION spriteToBitmapData
/**
* Converts BitmapData to a Sprite.
*
* @param bitmap The Bitmap that should be converted.
* @param smoothing Whether or not the bitmap is smoothed when scaled.
* @return The converted Sprite object.
*
* @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Bitmap.html#smoothing
*/
public static function bitmapDataToSprite(bitmapData:BitmapData, smoothing:Boolean = false):Sprite
{
var sprite:Sprite = new Sprite();
sprite.addChild(new Bitmap(bitmapData.clone(), "auto", smoothing));
return sprite;
} // END FUNCTION bitmapToSprite
// **********************************************************************************
// **********************************************************************************
} // END CLASS DisplayConverter
} // END PACKAGE
一旦你的位圖數據可以調用clone得到它的拷貝,創造的多個實例相同的圖像。 [做一些關於blitting的研究;遊戲開發者使用的技術]。 Spark Image標籤也將接受BitMapData作爲源。
你打算怎麼樣加載圖片?他們是否嵌入在Flex應用程序中?或通過URL加載?或者是其他東西? – JeffryHouser
我在代碼中使用image.source =「資源路徑」,否則我在設計器中使用源代碼按鈕,我相信它也會執行相同的操作。我會着手嵌入並找出這意味着什麼。 –
我已經做了一些更多的嵌入測試(只需使用設計器中的複選框)。它看起來像加載速度更快。現在,如果我有重複的s:使用相同圖像源的圖像,如果我多次嵌入相同的資源,是否會堵塞內存? –