Problem with refreshing an image dynamically

Hi !
I have a problem in my app (again :D).
In fact I want to load an image when I click on a button. Each click launch a function with the url of the image in parameter. I found a “loadImage” function in the forums and I tried to adapt it with what I need. Here it is :

--IMG is a url  
--TMPNAME is different each time I use the function  
--GROUPE is the group where I put the image in  
--X and Y are the coordinates of the newly loaded image  
  
function loadImage(IMG, TMPNAME, GROUPE, X, Y)  
  
 --try to erase the previous data, doesn't work...  
 path = nil  
 myfile = nil  
  
 path = system.pathForFile( TMPNAME .. ".png", system.DocumentsDirectory )  
 myFile = io.open( path, "w+b" )   
  
 -- Request remote file and save data to local file  
 http.request{   
 url = IMG,   
 sink = ltn12.sink.file(myFile),  
 }  
  
 -- Display local file  
 groupeFiche:remove(testImage)  
 testImage = display.newImage(TMPNAME .. ".png",system.DocumentsDirectory,X,Y);  
 GROUPE:insert( testImage )  
end  

My problem is that I want to overwrite the previous image with the new one each time I use this function, but from now on, each image is set on top of the previous…

Can a corona god help me ?
Thanks in advance !

Tonyo
[import]uid: 4431 topic_id: 1230 reply_id: 301230[/import]

Where did “groupeFiche” come from in “groupeFiche:remove(testImage)”? Should this be “GROUPE” instead?

Tom [import]uid: 6119 topic_id: 1230 reply_id: 3259[/import]

yeah, I put “groupeFiche” because it’s the groupe where I put the image in, but you’re right, it must be the parameter GROUPE. I’ll test this and keep you in touch of the results ! [import]uid: 4431 topic_id: 1230 reply_id: 3268[/import]

I’m not a Lua expert but I’m thinking that GROUPE is a local variable (because it’s a formal function argument) that goes away after the function exits and you no longer have access to the group.

I’ll be interested in hearing about the solution.

Tom [import]uid: 6119 topic_id: 1230 reply_id: 3270[/import]

Maybe you’re right, but the GROUPE:remove should work anyway in the function, and it doesn’t. I still have evry new image loaded on top of the previous… [import]uid: 4431 topic_id: 1230 reply_id: 3271[/import]

Okay, I think I figured out what’s wrong. GROUPE does reference your group but the reason you can’t remove the previous image is because you don’t have a reference to the previous image object. Each time you create a new image (display.newImage), that creates a new object. Even though you are using the same variable name, all you are doing is storing the new image object address in that variable.

The way to remove the previous image is by using a key into the Group. If you have three items in the group and the test image is the last item inserted, you can remove item 3.

Here is some example code:

 -- Display local file  
 if GROUP.numChildren == 3 then -- account for no image object the first time  
 GROUPE:remove(GROUPE[3])  
 end  
 testImage = display.newImage(TMPNAME .. ".png",system.DocumentsDirectory,X,Y);  
 GROUPE:insert( testImage )  
  

The other way would be to save the last test image object address in a global variable and use that to remove the image from the group. You would then save the new image address into the global variable.

Tom [import]uid: 6119 topic_id: 1230 reply_id: 3284[/import]

I found a cheesy solution… I put this in the beginning of my loadImage function :

 for i=1,GROUPE.numChildren do   
 local childW = GROUPE[i].width  
 local childH = GROUPE[i].height  
  
 if childW == 290 and childH == 210 then  
 --DOESN'T WORK  
 --GROUPE:remove(GROUPE[i])  
 --GROUPE:remove(i)  
  
 GROUPE[i].x = 900  
 print(GROUPE[i].x)  
 end  
 end  

Well, it works, but all the images are still in the group, so I think the app will slow down with all these useless media. I can’t understand why the group:remove doesn’t work… any idea ? [import]uid: 4431 topic_id: 1230 reply_id: 3291[/import]

I gave you the reason why you couldn’t remove the testImage object in my previous post. You can’t use the testImage name because it’s pointing to a different (newer) image. You either have to save the previous testImage value and use that to remove the object from the group or remove the last item added to group (using the GROUPE[n] value).

You can see this for yourself by printing out the testImage variable in your loadimage function: print(tostring(testImage))

Tom [import]uid: 6119 topic_id: 1230 reply_id: 3302[/import]

I think I just solved my probleme, thanks to your help Fogview !

In fact, in my function, I have a script that always resizes the images at the same width/height. Because I’m sure they’ll be at 290x210pixels, I made a if statement based on these 2 values. If I find a 290x210 image, I delete it and I load the new one. The code (whiwh didn’t work) was the following :

function loadImage(IMG, TMPNAME, GROUPE, X, Y)  
  
  
 for i=1,GROUPE.numChildren do   
  
 local childW = GROUPE[i].width  
 local childH = GROUPE[i].height  
  
 if childW == 290 and childH == 210 then  
 GROUPE:remove(i)  
 --GROUPE[i].x = 900  
 print(GROUPE[i].x)  
 end  
  
 end  
  
  
 path = system.pathForFile( TMPNAME .. ".png", system.DocumentsDirectory )  
 myFile = io.open( path, "w+b" )   
  
 -- Request remote file and save data to local file  
 http.request{   
 url = IMG,   
 sink = ltn12.sink.file(myFile),  
 }  
  
 testImage = display.newImage(TMPNAME .. ".png",system.DocumentsDirectory,X,Y);  
 GROUPE:insert( testImage )  
  
  
end  

I always had an error message that I didn’t understand.
For testing purpose, I added a test of GROUPE[i] value. Then I realized that each new image in the group doesn’t replace the position of a previous element (ex : I deleted the GROUPE[3] element, the new element will be the 4th).

So, to make the whole thing work, I made the following function :

function loadImage(IMG, TMPNAME, GROUPE, X, Y)  
  
  
 for i=1,GROUPE.numChildren do   
 if GROUPE[i] ~= nil then  
 local childW = GROUPE[i].width  
 local childH = GROUPE[i].height  
  
 if childW == 290 and childH == 210 then  
 GROUPE:remove(i)  
 --GROUPE[i].x = 900  
 print(GROUPE[i].x)  
 end  
 end  
 end  
  
  
 path = system.pathForFile( TMPNAME .. ".png", system.DocumentsDirectory )  
 myFile = io.open( path, "w+b" )   
  
 -- Request remote file and save data to local file  
 http.request{   
 url = IMG,   
 sink = ltn12.sink.file(myFile),  
 }  
  
 testImage = display.newImage(TMPNAME .. ".png",system.DocumentsDirectory,X,Y);  
 GROUPE:insert( testImage )  
  
  
end  
  

By adding the “if GROUPE[i] ~= nil” statement, I tested the values of the elements of the group, and the previous images are now deleted. GREAT.

Well, that’s the first step of my application, I have others problems that I’ll try to solve (like creating dynamic textfields with their position relative to the previous one position), thanks again ! [import]uid: 4431 topic_id: 1230 reply_id: 3304[/import]

I find that it really helps to add some print statements to code that not working. It will show you the state of variables so you can tell if you’re getting what you expect or if scope of the variable is wrong (nil).

Good luck.

Tom [import]uid: 6119 topic_id: 1230 reply_id: 3305[/import]