Issue using display.newImage() with a variable

I am trying to load a list of filenames into a table, and the use that table to create new images. The issue I am having is after the image table is loaded, the display.newImage() function will not work with a variable loaded from a file as an argument

Load Table Code
[lua]-- Load the Background Image Table
local path = system.pathForFile( “backgroundlevels.txt”, system.ResourceDirectory )
local counter = 1

for line in io.lines(path) do

globals.backgrounds[counter] = line
counter = counter + 1

end[/lua]

Select the Image to display
[lua]globals.currentScene = 1[/lua]

The contents of globals.backgrounds[globals.currentScene] is 1-1.png

Create Image code

[lua]function currentScene.loadCurrentScene()
local filePath = globals.backgrounds[globals.currentScene]
currentScene.background = display.newImage(filePath, 0 ,0)
end [/lua]

the error I am getting is below

b/c it does not exist.ath for resource file '1-1.png

)ARNING: Failed to find image(1-1.png
Runtime error
assertion failed!
[import]uid: 7664 topic_id: 1738 reply_id: 301738[/import]

Can you forward us code at support @ anscamobile.com so we can take a look at, including the text file.

carlos [import]uid: 24 topic_id: 1738 reply_id: 5163[/import]

I am having the same issue here - it’s not possible to load / create an image from a variable!

Here’s a simple code to demonstrate this issue (I am using functions in another module, but I don’t know if this makes any difference):
main.lua:
[lua]-- LOAD PACKAGE
local MyModule = require(“module1”)

MyModule.NewObject (“gfx.png”)

MyModule.DrawObjectImage()[/lua]

module1.lua:
[lua]module (…, package.seeall)

local Objects = {}

function NewObject(imagePath)
– CREATE A NEW OBJECT (ARROW SHAPE) AND ASSIGN AN IMAGE TO IT
Objects[1] = display.newLine(0,0, 16,0)
Objects[1]:setColor (255,255,255)
Objects[1]:append (8,-24, 0,0)
Objects[1].Image = display.newImage(imagePath,50,50) – PRELOAD IMAGE
Objects[1].Image.isVisible = false

end

function DrawObjectImage()
– THIS DOES NOT WORK!
local Gfx = display.newImage(Objects[1].Image, Objects[1].x,Objects[1].y)
end[/lua]

Results in error message:

ERROR: Argument(1) is not a GroupObject\ WARNING: Cannot create path for resource file '0'. File does not exist.\ \ WARNING: Failed to find image(0)\ Runtime error\ assertion failed!\ stack traceback:\ [C]: ?\ [C]: in function 'assert'\ ?: in function <?:518>\ (tail call): ?\ ...reigabe/Software/Corona\_Samples/Own/Test/module1.lua:19: in function 'DrawObjectImage'\ ...s/Freigabe/Software/Corona\_Samples/Own/Test/main.lua:6: in main chunk\ Runtime error: assertion failed!\ stack traceback:\ [C]: ?\ [C]: in function 'assert'\ ?: in function <?:518>\ (tail call): ?\ ...reigabe/Software/Corona\_Samples/Own/Test/module1.lua:19: in function 'DrawObjectImage'\ ...s/Freigabe/Software/Corona\_Samples/Own/Test/main.lua:6: in main chunk\ \ }
[import]uid: 9644 topic_id: 1738 reply_id: 8055[/import]

No wonder you are getting that error.

[lua]function DrawObjectImage()
– THIS DOES NOT WORK!
local Gfx = display.newImage(Objects[1].Image, Objects[1].x,Objects[1].y)
end[/lua]

You are not giving the filename as the parameter but the object handle that was stored in

[lua]Objects[1].Image = display.newImage(imagePath,50,50) – PRELOAD IMAGE[/lua]

The way you want to do it… copy an object… is not possible atm in Corona. You have to create an object with a file name. [import]uid: 5712 topic_id: 1738 reply_id: 8056[/import]

Ah, I see. I thought it would be possible to load an image once, store a handle in a variable and re-use this handle to create multiple copies of this image.

So an image has to be loaded each time I want to create one. But what if I would like to preload an image to avoid hickups during the game? Do I have to load one “dummy copy” of this image file first, set it to invisible and remain it on screen until the game ends? [import]uid: 9644 topic_id: 1738 reply_id: 8060[/import]

For small images, I have been loading them and making them visible or invisible as necessary. You can also just position them offscreen. Larger images like backgrounds I’ve been loading and removing during level scene changes to save texture memory.

My understanding though is that best practices for memory usage involve removing images that aren’t used, even smaller ones. [import]uid: 1560 topic_id: 1738 reply_id: 8067[/import]

In my case, I am using many (hundreds) of copies from a single image file and I want to prevent re-loading the same file from disk each time.

Since Corona “re-uses” loaded image data as far as I could find out here in the forums, it should be enough to load the needed graphic ONCE only, then hide it. As long, as the image data is still in memory (means: as long as at minimum ONE copy of this image is in use or in the display chain), this image will be loaded from memory, not from disk, when creating an image using the same graphic file.

This is my understanding. However, I hope I am right with this… ?! [import]uid: 9644 topic_id: 1738 reply_id: 8071[/import]

You are, unless something changed recently. :slight_smile: [import]uid: 5712 topic_id: 1738 reply_id: 8074[/import]