Hi,
I am trying to replace an image at runtime. It works the following way:
-
I have an image downloaded from a url in the docs directory
-
I use this image on several locations in my app
-
I download a new image replacing the existing one in the docs directory
-
I try to use the new image, but since the old one is loaded in memory and has the same filename, display.newImage will load the old file instated.
Is there a way to remove the old image reference from memory?
Here is a sample code illustrating my problem
[lua]
local function downloadImage(localImageUrl, directory, imageUrl, onComplete) --download an image form imageUrl to localImageUrl and directory
local function onImageSaved(event)
if “ended”==event.phase then
if event.isError then
onComplete(nil)
else
onComplete(localImageUrl)
end
end
end
network.download(
imageUrl,
“GET”,
onImageSaved,
localImageUrl,
directory
)
end
local previousImage
local image
local file = “myimage.png”
local directory = system.DocumentsDirectory
local url1 = “http://www.cubadebate.cu/wp-content/uploads/2011/01/torre-eiffel.png”
local url2 = “http://icons.iconarchive.com/icons/iconka/places-of-interest/256/eiffel-icon.png”
local function removeImage()
image:removeSelf()
image=nil
end
local function displayImage()
image=display.newImage( file, directory, 0, 0, true )
end
local function loadTwoImages()
--load previous image
previousImage=display.newImage( file, directory, 0, 0, true )
previousImage.isVisible=false --it is hidden, no to distorb
displayImage() --load the same image again, this time visible
end
downloadImage(file, directory, url1, loadTwoImages) --download image1, display it on complete
local function replaceImage()
removeImage() --remove the existing image
downloadImage(file, directory, url2, displayImage) --downlaod and display the new one. Does not make any effect
end
timer.performWithDelay(3000, replaceImage) --after 3 secs replace the image
[/lua]
Thanks!