Jump to content
Search Community

LoaderMax ImageLoader clarrification

Chris7777 test
Moderator Tag

Recommended Posts

As a test for loader max, i started building a small air app (i thought it would be interesting to test in a non typical flash environment).

 

The app reads my external hard drive for all movie files, and then checks a particular folder for their thumbnail, based just on filenames (i.e. Movie A.avi = Movie A.jpg). Pretty simple stuff.

 

The folder contains approx. 250 movies, but only approx. 100 images, so where the image wasn't found, I wanted to load a "missing.jpg" image. A generic image.

 

So simple pseudo code

 

loop through each movie
   loop through image directory
     if image found then
         queue.append(new image loader(image path))
     else 
         queue.append(new image loader(missing image path))
   end loop
end loop 
process queue

 

This could obviously be done multiple ways. Actually probably a lot more efficient to load the missing image once, then not append if the image isn't found, but that isn't the point.

 

My question is, that is appears that it attempts to load the missing image every time. I'm pretty sure flash automatically cache's loaded resources, but how does loader max handle multiple loads of the same resource?

 

Just out of interest?

Link to comment
Share on other sites

Good question. You're right - assets are typically cached by the browser/player so even if you load the same image 100 times, you shouldn't have to worry about it going out to the web server each time and wasting bandwidth. It would load from the server the first time and then all subsequent loads of the same url would be pulled from the browser/player cache unless you set noCache:true in your loader.

 

That being said, LoaderMax does not assume that the same url should always be mapped to exactly the same resource internally. That could cause other problems, like if you were going to display a grid of 50 images and 30 of them were the same url (like your missing.jpg example), mapping them all to a single Bitmap internally would mean that when you addChild() to build your grid, it'd keep adding the same instance so you'd end up with a bunch of empty slots since an instance can only exist in one place at a time. See what I mean?

 

So did LoaderMax work well for you in your sample AIR app?

Link to comment
Share on other sites

Yes exactly - as any display object can only exist on one display list. In the end, missing.jpg was stored in the library as BitmapData (I could load it in as unique one off at the start of the queue though).

 

In terms of LoaderMax, it handled everything easily! - It was as if all the items were pretty much stored in the library ready to go, I didn't really have to think about it. (which is want you want from a Loading Platform).

 

It was quite interesting to feed the File nativePath property directly into ImageLoader to bring in images from the drive straight into the Air app. Could lead to some very powerful stuff.

 

 

I included some sample code of what I was using LoaderMax for - I just wanted to create a mini media browser. Obviously needs a lot of work, but a good proof of concept.

 

import flash.filesystem.File;
import flash.events.FileListEvent;

import com.greensock.*;
import com.greensock.loading.*;

import flash.events.*;

// The 2 dir's
var movieDir:File = new File("F:/movies/");
var thumbnailDir:File = new File("F:/movies/_thumbnails/");

// Get files in dir's
var movieArray:Array = movieDir.getDirectoryListing();
var thumbnailArray:Array = thumbnailDir.getDirectoryListing();

// Sort them into alphabetical order
movieArray.sortOn("name")

// Store length in int's to same looking up everytime
var numMovies:int = movieArray.length;
var numThumbnails:int = thumbnailArray.length;

// Create queue
var queue:LoaderMax = new LoaderMax({onComplete:completeHandler}); 
var container:Sprite = new Sprite();

stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;

// Populate if the movie isn't a directory, and isn't that annoying Thumbs.db.
// movie.avi will match to movie.jpg (hence the substrings)
if(movieDir.exists && thumbnailDir.exists) {
for(var i:int = 0; i < numMovies; i++) {
	if(movieArray[i].name != "Thumbs.db" && !movieArray[i].isDirectory) {
		for(var j:int = 0; j < numThumbnails; j++) {
			if(movieArray[i].name.substr(0, movieArray[i].name.lastIndexOf(".")) == thumbnailArray[j].name.substr(0, thumbnailArray[j].name.lastIndexOf("."))) {
				queue.append(new ImageLoader(thumbnailArray[j].nativePath));
			}
		}
	}		
}
}

// Once populated - load
queue.load();


function completeHandler(event:Event):void {
var tb:Sprite;
var t:TextField;
var tf:TextFormat = new TextFormat();
tf.font = "Myriad Pro"; // Embed Font in library exported to AS as MyriadPro
tf.size = 20;
tf.color = 0xFFFFFF;

for(var i:int = 0; i < numMovies; i++) {
	tb = queue.getContent("F:\\movies\\_thumbnails\\" + movieArray[i].name.substr(0, -4) + ".jpg");
	if(!tb) {
		tb = new Sprite();
		tb.addChild(new Bitmap(new Missing())); // Missing contained in library as jpg extending BitmapData
	}
	// Position thumbnail
	tb.width = 100; // Set thumbnail size (as jpg's vary)
	tb.height = 200;
	tb.y = (200 + 10) * i;

	// Setup text
	t = new TextField();
	t.defaultTextFormat = tf;
	t.embedFonts = true;
	t.autoSize = TextFieldAutoSize.LEFT;
	t.text = movieArray[i].name.substr(0, -4);
	t.y = (200 + 10) * i + 60;
	t.x = 150;		

	// Add thumbnails and text to container
	container.addChild(t);
	container.addChild(tb);
}	

addChild(container);

// Once loaded, handle interaction
container.addEventListener(MouseEvent.CLICK, handleClick, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_WHEEL, handleWheel, false, 0, true);
}

// Click opens the movie with the default ap (VLC in my case)
function handleClick(e:MouseEvent):void {
movieArray[int(container.mouseY/200)].openWithDefaultApplication();	
}

// Rolling the mouse tweens the container up and down (no upper boundary)
function handleWheel(e:MouseEvent):void {
var v:Number = container.y + (e.delta * 70);
if(v > 0) v = 0;
TweenLite.to(container, 0.5, {y:v});	
}

 

I guess its worth noting, considering this is about LoaderMax that all it took was 2 lines of code to load in hundreds of images the constructor line (new LoaderMax) and the append line (queue.append)

 

Thanks Jack - when are Adobe giving you a job already?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...