From f58d06845b8c699eda1053500270949ad5d6ee4f Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Mon, 23 Jan 2017 13:40:25 -0800 Subject: [PATCH] Implement Image.loadFromBytes on Flash, guard against null data on load --- lime/graphics/Image.hx | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lime/graphics/Image.hx b/lime/graphics/Image.hx index 88b896e74..300c36de9 100644 --- a/lime/graphics/Image.hx +++ b/lime/graphics/Image.hx @@ -740,6 +740,8 @@ class Image { public static function loadFromBase64 (base64:String, type:String):Future { + if (base64 == null || type == null) return Future.withValue (null); + var promise = new Promise (); #if (js && html5) @@ -781,6 +783,8 @@ class Image { public static function loadFromBytes (bytes:Bytes):Future { + if (bytes == null) return Future.withValue (null); + #if (js && html5) var type = ""; @@ -805,6 +809,34 @@ class Image { return loadFromBase64 (__base64Encode (bytes), type); + #elseif flash + + var promise = new Promise (); + + var loader = new Loader (); + + loader.contentLoaderInfo.addEventListener (Event.COMPLETE, function (event) { + + promise.complete (Image.fromBitmapData (cast (loader.content, Bitmap).bitmapData)); + + }); + + loader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS, function (event) { + + promise.progress (event.bytesLoaded, event.bytesTotal); + + }); + + loader.contentLoaderInfo.addEventListener (IOErrorEvent.IO_ERROR, function (event) { + + promise.error (event.error); + + }); + + loader.loadBytes (bytes.getData ()); + + return promise.future; + #else return new Future (function () return fromBytes (bytes), true); @@ -816,6 +848,8 @@ class Image { public static function loadFromFile (path:String):Future { + if (path == null) return Future.withValue (null); + #if (js && html5) var promise = new Promise ();