Can you store images in a table?

The title is the question, I tried adding images to a table but it doesn’t seem to do anything when I use sprite(imgtbl[1]) which is a problem so is there any way to work around it?

show your code please, because for me, that work. You need to use readImage() like this :

imgtbl = {}
imgtbl[1] = readImage(spriteLocation)

```

Ahh I got you, that helped i was using image = image instead of image = image:copy() thanks

Is there a way to stop frequent use of copying large images in to a table from crashing?

Are you trying to take a large image and break it up into smaller images? The best way to go about doing this is to use a mesh. Use a table to store the locations of the smaller images on the mesh. Below is an example of taking a large image and splitting it into x*y equal parts.

Create the tables

tilesets = {
        { sheet = readImage("imgLocation"), x = 8, y = 4 }
}

tiles={
        { set = 1, x = 0, y = 3 },
        { set = 1, x = 0, y = 2 },
        { set = 1, x = 0, y = 1 },
        { set = 1, x = 0, y = 0 },
        { set = 1, x = 1, y = 3 },
        { set = 1, x = 7, y = 3 }
}

m = {}

Create the mesh

for t=1,#tiles do
    if m[t] == nil
        m[t] = mesh()
        m[t].texture = tilesets[tiles[t].set].sheet
    end
    w,h = 1/tilesets[tiles[t].set].x,1/tilesets[tiles[t].set].y
    tx,ty = tiles[t].x*w,tiles[t].y*h
    img = m[t]:addRect(xCoordOnScreen,yCoordOnScreen,width,height)
    m[t]:setRectTex(img,tx,ty,w,h)
end

Draw the mesh

m[1]:draw()
m[2]:draw()
etc.

For addRect, you will need to determine where the smaller image is placed on screen and the size of it in pixels.

@Luatee - what exactly are you doing with the images, and how big are they?

What I’ve done is made a drawing app using an image the size of the screen that gets drawn to with setContext and I store the image to a table using image:copy because I wanted to put in an undo button but if I do this fast when drawing with a pen or such it causes codea to crash from what I’m guessing is some sort of overflow of memory, any ideas?

@Luatee - I just caught a really interesting article on why JS sucks on mobile, but in the middle was an example showing how images can suck up your memory

http://sealedabstract.com/rants/why-mobile-web-apps-are-slow/

Scroll down about halfway to HOW MUCH MEMORY IS AVAILABLE ON IOS, and there is a really interesting discussion of iPad memory constraints wrt images.