Mint ImageLoader class.
- MintPaw
-
MintPaw
- Member since: Jun. 11, 2006
- Offline.
-
- Forum Stats
- Member
- Level 10
- Programmer
Back in the Flash Player 9 days I wanted to let the user upload images into the my flash games and let him create the players logo in my game. Sadly back then I realized it was imposable without some kind of server to hold the images.
Look like a while ago it suddenly became possible with Flash Player 10, and I never knew, but it is a bit tricky. So I took the time to write a class that people might find useful.
Links are all below, in the classes link you'll find a folder called Mint, take that folder and move it into your source folder or a class path directory. It contains ImageLoader and ImageLoaderEvent.
Here's an example of how it works:
Make sure to import import Mint.*;
//Vars
private var _imageLoader:ImageLoader;
private var _loadText:TextField;
//My init function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//Create a text box and add it to the centre of the stage.
_loadText = new TextField();
_loadText.text = "Load image";
_loadText.x = stage.stageWidth / 2;
_loadText.y = stage.stageHeight / 2;
_loadText.selectable = false;
addChild(_loadText);
//Add an event listener to start the file load when it is clicked.
_loadText.addEventListener(MouseEvent.CLICK, openFile);
}
private function openFile(e:MouseEvent):void
{
//Create an new instance of the image loader
_imageLoader = new ImageLoader();
//Add an event listener to listen for the upload being complete. Doesn't take really any time at all.
//This uses the IMAGE_LOADED Event in the ImageLoaderEvent class.
_imageLoader.addEventListener(ImageLoaderEvent.IMAGE_LOADED, displayImage);
//Start the image request.
_imageLoader.requestImage();
}
private function displayImage(e:ImageLoaderEvent):void
{
//Create a new Bitmap the hold the returned Bitmap.
//The Bitmap data is returned in the event under .bitmapData, so we can get it by using e.bitmapData
//In this case we're going to put it directly into a Bitmap.
var bitmap:Bitmap = new Bitmap(e.bitmapData);
//Then addChild the Bitmap.
addChild(bitmap);
//For the example's sake, move the Bitmap under the "Load Image" text so you can use it again.
setChildIndex(bitmap, getChildIndex(_loadText));
}
This will not work if you're trying to request an image outside of a user initiated event. So ImageLoader cannot run in something like a constructor, in this case it's in a MouseEvent so it works correctly, otherwise it would be a security issue and simply fail. So do test this in a browser if you're planning on using this in a game.
Links:
Swf test
Classes
Full example(FlashDevelop project)
Use it however you wish, no need to credit.
- swishcheese
-
swishcheese
- Member since: May. 12, 2007
- Offline.
-
- Forum Stats
- Member
- Level 14
- Programmer
- JeremysFilms
-
JeremysFilms
- Member since: Feb. 18, 2005
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate

