Create image from data-in-uri (base64-encoded PNG) in ActionScript
I have a string of of base64-encoded PNG image that is suitable to use as a src (source) attribute in a <img>
tag. However I need to transfer this image to Flash applet where I need to create (show) the image fast. Is there a way to simply use the data string and somehow create an image (inside a Flash movie) from it?
Performance of the operation is a core 开发者_JAVA技巧requirement.
At the very least you must decode the base-64 data into binary. You can then load the binary data as an image using Loader.loadBytes().
Here's a fast base64 decoder compiled with alchemy: http://www.blooddy.by/en/crypto/
var bytes:ByteArray = by.blooddy.crypto.Base64.decode(string);
var loader:Loader = new Loader();
loader.loadBytes(bytes);
Update: You can use the "Textfield.getImageReference()" to gain access to the Loader for tag. Once you have it you can tell it to load the image bytes, like this:
textfield.htmlText = 'hello <img id="myImage" src="" /> hello';
var image:DisplayObject = textfield.getImageReference("myImage");
var loader:Loader = image as Loader;
loader.loadBytes(bytes);
Here's a complete end-to-end example:
var textfield:TextField = new TextField();
addChild(textfield);
textfield.htmlText = 'hello <img id="myImage" src="" /> hello';
var data:String = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAKqUlEQVRYhe2YWYwc13WGv1q7urt6n+nZF5IzJDWkSCoiRY1MiZToBbYUCrJgx7ADIwECI0igLDYCPSuAEyR5SWIndgzHeTJhSIptGJZs05YFm9o4EmXOkCJFzr5vvVVXd1XXngfSgRWK9JBiHgLkAPVyq07d7/73nnvOvfD/9sFMuF3H+3skKZ9JHm1vSx0z6va7a5vGs28sB/6dhAOQb9cxm058ae9w1zOdxYwmSlL00isX8ywbX72TcADSrToc6pLUu/tSX9s92PG0piqy7wd0FDOC6/kHM1Hj+elKUPvN7z8+kto5kJESsxW/fjuA4q06FDLxPxrozP1hFEVXfyAKOI5HPqsXQ8QT9/e8d8zdxcy32nKJv//s/sTt8N36FKcS6p/HNUVuWA5hGFE2AtYqBo7vk23T/85crP4UuATw8eHYtu6O3IfWNqhsrDXvLOAT+3V517bs59WYMry2YT4/caV69o3lgGrT1StzJbSYSCIuE4tJqKKEKov0Fn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FQICljyBqigS+QLHWvMUSiabywYHExErb9k88lGFpZLGTyYHueLto+lWkFUVgJRoIvgB1ZJFreFhCD4NP7wugrcE+MWjhb69946OnH13BjVwyHel6RsY5EzV8srdH1GSmsTDjyxiz8yxoy+FFpMorEQ8ezrBy+LH8EIBz2nhlC+ihTXsgYdpqgpWrUmrbjNXcwgyghGEXLllwKdG9YHBod2viAK9hb6+yaX5mRnRjR0/OHpYPjv5n8pYwyKT7YEwoFiIszJXoewEpDWRvriDuV4hcAwU8zJ5e/qK7zuzob36yFRml/KyW6Y/FeKkBL9ihX+dUIXGLQE+NaoLqUzuH/q3D/euLMzOXpo4+yCwUezses5znScPDHXw1rkpqsk0ZdPHWjJYnCxRtXy0uEw2o5Hd/BGRU19rtuyfXzGCM14QOZ2pxYv51uros2WpLyH5AYLwL6mY8NXvj9vvy3EzBfu7ewce1hJJjGr5G8D6V15v8EzH6nzPwBCKItWSjXca5XJP76uTIY8VVCpNn5YX0vA8ujMylrHx9amK/yugCYwDC6tm0Fo1g1/329rVJoevzt/4WvFm5ZaSzuWVMAho2ZZyTdV8vq3jU3WjQq3pTpZr1ZOp2uve98cjHElAick0nIBQELC8gIYXVYAq8AJwAagDLmBde8LLpZvfed4wkxzuUxu5QvtDnuvs2LnnwMMjBw79Xs/A9mcEQWwrlUrlb7749j8v14MZLTSXNH9z++SGrx87kCOeiHHPcIZfTlWab6+6pxw/+u41yNuyGwKOLbnBsFY9HdPivVPvnk8360bcbjaMRr128vSvLn/xjTnrFHDRdKMLtutMRq4lbBhWerBNNc/N18xXFqyJBSP4xzBi4XbhYAvl1lOj+nVtX3n9+oArJkW252WG8zIVO2SuFvDOxm+/Ofg/b/8FrV74eebdEP8AAAAASUVORK5CYII=";
var bytes:ByteArray = by.blooddy.crypto.Base64.decode(data);
var image:DisplayObject = textfield.getImageReference("myImage");
var loader:Loader = image as Loader;
loader.loadBytes(bytes);
精彩评论