
Код AS3:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.utils.Dictionary;
public class ImageChache extends Sprite
{
private const bitmapDataByURL:Object = new Object();
private const bitmapsByURL:Object = new Object();
private const urlByLoaderInfo:Dictionary = new Dictionary();
public function ImageChache ()
{
stage.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick (event:MouseEvent):void
{
while (numChildren > 0)
{
removeChild(getChildAt(0));
}
addChild(getImage("http://www.flasher.ru/forum//image.php?u=32784&dateline=1321357870"));
}
public function getImage (url:String):Bitmap
{
var bitmap:Bitmap = new Bitmap();
var bitmapData:BitmapData = bitmapDataByURL[url];
if (bitmapData == null)
{
var bitmaps:Vector.<Bitmap> = bitmapsByURL[url];
if (bitmaps == null)
{
bitmaps = new Vector.<Bitmap>();
bitmapsByURL[url] = bitmaps;
bitmaps.push(bitmap);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
urlByLoaderInfo[loader.contentLoaderInfo] = url;
loader.load(new URLRequest(url));
return bitmap;
}
bitmaps.push(bitmap);
return bitmap;
}
bitmap.bitmapData = bitmapData;
return bitmap;
}
private function onLoadError (event:ErrorEvent):void
{
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
loaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
trace(event.text);
}
private function onLoadComplete (event:Event):void
{
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
loaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
var url:String = urlByLoaderInfo[loaderInfo];
var bitmapData:BitmapData = (loaderInfo.content as Bitmap).bitmapData;
var bitmaps:Vector.<Bitmap> = bitmapsByURL[url];
for each (var bitmap:Bitmap in bitmaps)
{
bitmap.bitmapData = bitmapData;
}
bitmapDataByURL[url] = bitmapData;
delete bitmapsByURL[url];
delete urlByLoaderInfo[loaderInfo];
}
}
}