在ActionScript中从data-in-uri(base64编码的PNG)创建图像

时间:2011-01-11 08:52:39

标签: html flash image actionscript base64

我有一串base64编码的PNG图像,适合用作<img>标记中的src(source)属性。但是我需要将此图像传输到Flash applet,我需要创建(显示)图像快速。有没有办法简单地使用数据字符串,并以某种方式从它创建一个图像(在Flash电影内)?

操作的性能是核心要求。

1 个答案:

答案 0 :(得分:2)

至少你必须将base-64数据解码为二进制数据。然后,您可以使用Loader.loadBytes()将二进制数据加载为图像。

这是一个用alchemy编译的快速base64解码器:http://www.blooddy.by/en/crypto/

var bytes:ByteArray = by.blooddy.crypto.Base64.decode(string);
var loader:Loader = new Loader();
loader.loadBytes(bytes);

更新:您可以使用“Textfield.getImageReference()”来访问Loader for tag。一旦你拥有它,你可以告诉它加载图像字节,如下所示:

textfield.htmlText = 'hello <img id="myImage" src="" /> hello';
var image:DisplayObject = textfield.getImageReference("myImage");
var loader:Loader = image as Loader;
loader.loadBytes(bytes);

这是一个完整的端到端示例:

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/85crP4UuATw8eHYtu6O3IfWNqhsrDXv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JdKerNj+BbTdyWTdSYJb1bwWt5zC8YbDoecvz9V9uWAE3bq166vI4aRbSlFQICljyBqigS+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);