I decided to go with image.load() and gui.new_texture() method and it works with my other code:
local function set_background_image(texture_id, image_filename)
background_node = gui.get_node("info_screen_background")
-- check if image file exists
local file_exists = lfs.attributes(image_filename, "mode") == "file"
if not file_exists then
gui.set_texture(background_node, "background")
print("Background image file does not exist.")
else
local file = io.open(image_filename, "rb")
local bytes = file:read("*a")
local background_image = image.load(bytes)
math.randomseed(os.time())
_texture_id = texture_id .. "_" .. math.random()
if gui.new_texture(_texture_id, background_image.width, background_image.height, background_image.type, background_image.buffer) then
gui.set_texture(background_node, _texture_id)
print("Set new texture")
else
gui.delete_texture(_texture_id)
gui.set_texture(background_node, "background")
print("Unable to create texture")
end
end
end
However, Im interested about memory management. When image is loaded, does it stay in memory and should I delete it with gui.delete_texture after being replaced with other image?