I was in the process of loading the resources used by gui from my private server using Live Update. There is too much atlas used by gui (about 1440x800 pixels with more than 100), causing the editor to stop.
So I decided to change the way. As soon as I run apk, I get the resources used by the gui from my private server. Below is a function that loads an image from the server.
function list_image_download(self, list_table)
local image_table = {}
for i = 1, #list_table do
-- 0 is not exist, 1 is exist
if list_table[i].exist == 0 then
http.request(URL .. "images/" .. list_table[i].name, "GET", function(self, _, response)
if response.status >= 200 or response.status < 300 then
local image = image.load(response.response)
if not image then
print(list_table[i].name .. " unable load")
return
end
list_table[i].exist = 1
-- i need a save process ..
else
print("IMAGE RESOURCE LOADING FAILED")
return
end
end, headers)
end
end
end
I succeeded in downloading the image, but could not proceed to the next. Can I save this png file to my phone? In addition, what happens to the saved files when I delete the application? Help me ~~
Yes, if you use the Lua io.* functions you can save and load files. Untested example:
local f = io.open("foo.png", "wb")
f:write(response.response)
f:flush() -- possibly also flush before closing
f:close()
local f = io.open("foo.png", "rb")
local s = f:read("*a")
f:close()
image.load(s)
You need to write to a folder where the user and the app has write permissions. Change to:
local path = sys.get_save_file("myapp", "foo.png")
local f = io.open(path, "wb")
Note that âmyappâ should be replaced by a name unique to your game! Perhaps use the project title (and maybe get it as sys.get_config("project.title")
Thank you for all your help. @britzl!!
I downloaded png, saved it, and loaded it. But I also have two questionsâŚ
The first is the âWARNING: GUI: Failed to create dynamic gui texture (-6)â issue in gui.new_texture. Iâm developing the action to bring the stored image when I touch it. The first touch will be gui.set_texture, but it will not work after that.
WARNING:GUI: Failed to create dynamic gui texture (-6)
DEBUG:SCRIPT: ICON NEW TEXTURE ERROR!
local path = sys.get_save_file("image_speaker", icon .. ".png")
local f = io.open(path, "rb")
local s = f:read("*a")
f:close()
local img = image.load(s)
if gui.new_texture("icon", img.width, img.height, "rgba", img.buffer) then -- error here
gui.set_texture(node, "icon")
else
print("ICON NEW TEXTURE ERROR!")
end
The second is about the image type. I downloaded a png file with an alpha value, but the type of the saved png file is rgb. Is there a problem saving the image?
âiconâ used in gui.new_texture() represents the texture id. This must be unique and -6 indicates that there already is a texture with that id. You have a couple of options:
Call gui.delete_texture()
Reuse the same texture and only call gui.set_texture_data()
Select another texture id
Hmm, this sounds strange. If you are downloading using http.request and saving using io.write then there is no way that the transparency could get lost.
Perfect! The first issue was resolved by reusing the texture, and the second issue was the problem with the original image file. You are my hero. Thanks @britzl!