Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CachedImage extends Sprite
- {
- private var dir:String;
- private var loader:Loader;
- public function new(w:Float = 0, h:Float = 0, path:String)
- {
- super();
- this.graphics.beginFill(0xFFFFFF, 0.1);
- this.graphics.drawRect(0, 0, w, h);
- dir = SystemPath.applicationStorageDirectory + "\\" + resolvePath(path);
- var splitDir:Array<String> = dir.split("\\");
- createFolder(splitDir.splice(0, splitDir.length - 1).join("\\"));
- loader = new Loader();
- addChild(loader);
- loader.alpha = 0;
- loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
- var request:URLRequest = new URLRequest();
- if (FileSystem.exists(dir))
- {
- trace("load from cache");
- request.url = dir;
- }
- else
- {
- trace("load from url");
- request.url = path;
- }
- loader.load(request);
- }
- private function createFolder(s:String):Void
- {
- if (!FileSystem.exists(s))
- {
- try
- {
- FileSystem.createDirectory(s);
- }
- }
- }
- private function onLoaderComplete(e:Event):Void
- {
- loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaderComplete);
- Actuate.tween(loader, 0.5, { alpha:1 } );
- if(!FileSystem.exists(dir))
- {
- try
- {
- File.saveBytes(dir, loader.contentLoaderInfo.bytes);
- }
- }
- }
- public static function resolvePath(path:String):String
- {
- path = StringTools.replace(path, "http://", "");
- path = StringTools.replace(path, "https://", "");
- path = StringTools.replace(path, "/", "\\");
- return path;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement