I want to re-load a remote inage each time I go into that scene

Hello there Corona Forum people.  1st post so go easy on me :slight_smile:

I’m sure it’s been asked many times already and I have searched the forums and internet as a whole for a solution but as yet have not found one.

I have a storyboard with several scenes on, one in particular loaded a remote image [loadRemoteImage] when I click on that scene.

I had issues whereby if another one of the scenes was clicked the image would still show on that scene.  I tried all manner of ways to try and remove it from memory etc but could not find a way.  In the end I opted for io.open and then displayed that image via display.newImageRect

This seems to now remove okay when I click on one of the other scenes.  1st issues sorted, maybe!

2nd issue is to now have a way of re-loading the image if I go back into that scene.  I’m pretty sure I need to remove, purge, nil that image somehow but cannot for the life of me figure it out.  Here is the code for that scene and if you can point me in the right direction I’d be eternally grateful.

I just need to get know how to remove, purge the image when I exit that scene so it reloads correctly on going back into that scene.  Thank you.  Here’s the code

[lua]

–[[ $Id$

–]]
– Load the relevant LuaSocket modules
local http = require(“socket.http”)
local ltn12 = require(“ltn12”)
local bsc = require “xxxxxx.interface.bsc”
local ui = require “xxxxxx.interface.ui”
local storyboard = require “storyboard”
local scene = storyboard.newScene()

– Create local file for saving data
local path = system.pathForFile( “snapshot.jpg”, system.DocumentsDirectory )
myFile = io.open( path, “w+b” )
 

– forward declarations
local object1

–createScene
function scene:createScene( event )
   local group = self.view

   – Request remote file and save data to local file
http.request{
    url = “http://xxx.xxx.xxx.xxx/xxx/snapshot.jpg”,
    sink = ltn12.sink.file(myFile),
}
   
   object1 = display.newImageRect( “snapshot.jpg”,system.DocumentsDirectory, 300, 230 )
    object1:setReferencePoint( display.CenterReferencePoint )
    object1.x = display.contentCenterX
    object1.y = display.contentCenterY
   

   group:insert( object1 )

end
scene:addEventListener( “createScene”, scene )

–enterScene
function scene:enterScene( event )
   local group = self.view

       object1.isVisible = true

end
scene:addEventListener( “enterScene”, scene )

–exitScene
function scene:exitScene( event )
   local group = self.view

end
scene:addEventListener( “exitScene”, scene )

–destroyScene
function scene:destroyScene( event )
   local group = self.view

end
scene:addEventListener( “destroyScene”, scene )

return scene

[/lua]

This tutorial/blog post might be of interest to you: http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

It covers why this isn’t working for you.  The gist of it is the code at lines 11 and 12 are in the scene’s main chunk and only execute once for that scene’s life unless you un-require the scene.  The next time you visit this scene, those lines will not execute.  Secondly, depending on what you’re doing, there is a good chance that createScene() may not run a second time as well.  We try to keep old scenes in memory for efficiency.  Unless the scene’s view is destroyed, createScene won’t run again.

The blog post could cover most of it.  It is a video tutorial because some of it just can’t be demo’ed in text only.

Rob you really are a Miracle, thank you for pointing me in the right direction.  I’ve now finally got it work as expected.  I had seen that video but was unsure whether it was relevant to what I wanted to do.

Thank you again

Si

This tutorial/blog post might be of interest to you: http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

It covers why this isn’t working for you.  The gist of it is the code at lines 11 and 12 are in the scene’s main chunk and only execute once for that scene’s life unless you un-require the scene.  The next time you visit this scene, those lines will not execute.  Secondly, depending on what you’re doing, there is a good chance that createScene() may not run a second time as well.  We try to keep old scenes in memory for efficiency.  Unless the scene’s view is destroyed, createScene won’t run again.

The blog post could cover most of it.  It is a video tutorial because some of it just can’t be demo’ed in text only.

Rob you really are a Miracle, thank you for pointing me in the right direction.  I’ve now finally got it work as expected.  I had seen that video but was unsure whether it was relevant to what I wanted to do.

Thank you again

Si