Loading...
Logo
Processing Forum

array of images to large

in Programming Questions  •  8 months ago  
hi,
Im loading an array of images, but I want to go trough a large number of images, 110,

the problem is that if my array have more than 12 elements I get a memory error,

I dont want just to increase the memory available, as I know im doing this in a non optimal way,

so how can I refactor my array?, or what to do to show my image gallery?

thanks,
 
please find the code here:
Copy code
  1. PImage [] images = new PImage[10]; //more than 12 images out of memory!!

  2. int imageCount = 1;

  3. void setup () 
  4. {
  5.   size(displayWidth,displayHeight);

  6.   for (int i = 1 ; i<images.length; i++)
  7.   {
  8.     images[i] = loadImage("/images/"+i+".jpg");
  9.   }
  10. }

  11. void draw () 
  12. {
  13.   background (0); 
  14.   if (imageCount == 0) {
  15.     imageCount = images.length-1;
  16.   }
  17.   
  18.   if (imageCount == images.length) {
  19.     imageCount = 1;
  20.   }

  21.   println ("my image count is on::"+imageCount);
  22.   image (images[imageCount], (displayWidth - images[imageCount].width)/2, (displayHeight - images[imageCount].height)/2);
  23. }

  24. void keyPressed()
  25. {
  26.   if (key == CODED) {
  27.     if (keyCode == UP) {
  28.       println("up");
  29.       imageCount++;
  30.     }
  31.     if (keyCode == DOWN) {
  32.       println("DOWN");
  33.       imageCount--;
  34.     }
  35.     if (keyCode == RIGHT) {
  36.       println("RIGHT");
  37.     }
  38.     if (keyCode == LEFT) {
  39.       println("LEFT");
  40.     }
  41.   }
  42. }

Replies(2)

What are the sizes of your image ? (remember your image are unpacked after loading so a 1024*1024 image would take 4 Megs of Ram)

However may be you should try to dynamically load the previous N and next N images + the current one and destroy the others in an Arraylist ? (I hope you see what I mean)
As said...
You can also try and put  g.removeCache( images[imageCount]); after the image() call. It will reduce the memory usage, perhaps at the cost of a small slowdown. (If you use 2.0b, at least.)