iggyvolz
iggyvolz

Reputation: 101

AS3 - Convert String to Bitmap

I'm trying to store my images on a server to lighten the load of my SWF file. I've spent all day trying to get the data from the server, but all I get is a seemingly random string (which I understand is the string value of my image). I've tried reading up on this, but I haven't found anything that actually works, I get a lot of references to Base 64 encoding/decoding and I've tried a couple of libraries but they do nothing. Here's what I have (I've simplified it a bit to upload it):

public function loadImage() : void {
    var url:String='example.com/some_image';
    var load:URLLoader=new URLLoader(new URLRequest(url));
    load.addEventListener(Event.COMPLETE, onLoadComplete);
}

public function onLoadComplete(event : Event) : void {
    var imgString:String=event.target.data;
}

But how do I convert imgString to a Bitmap or Sprite so I can addChild() it? I've tried imgString as Bitmap which returns null. Maybe it has something to do with the BitmapData class? This post seems similar but is unanswered.

Upvotes: 0

Views: 361

Answers (1)

Marty
Marty

Reputation: 39458

If you have a server that runs PHP, you can generate the image first, e.g.

// PHP script 'http://yoursite.com/image.php
header("Content-type: image/jpeg");
echo $myImageDataFromDB; // Get the image data from MySQL.

And then in ActionScript, use the standard process to load that as an image:

var request:URLRequest = new URLRequest("http://yoursite.com/image.php");
var loader:Loader = new Loader();

loader.load(request);
addChild(loader);

Upvotes: 1

Related Questions