Unload Images from memory

Hi,

I am trying to replace an image at runtime. It works the following way:

  1. I have an image downloaded from a url in the docs directory

  2. I use this image on several locations in my app

  3. I download a new image replacing the existing one in the docs directory

  4. 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!

Have you tried this on the actual device?  I had an issue one time where the simulator was caching images and not cooperating with what I wanted to do.  If that doesn’t work try actually deleting the current image file before downloading the new one.  

@JonPM did you notice the “previousImage” variable? This one is never removed and is causing the issue.

What I want is to somehow force the system to remove the image from memory. Something like accessing to a table reference and setting it to nil.

Thanks!

I’m having the same problem. Was this resolved? Thanks.

Have you tried this on the actual device?  I had an issue one time where the simulator was caching images and not cooperating with what I wanted to do.  If that doesn’t work try actually deleting the current image file before downloading the new one.  

@JonPM did you notice the “previousImage” variable? This one is never removed and is causing the issue.

What I want is to somehow force the system to remove the image from memory. Something like accessing to a table reference and setting it to nil.

Thanks!

I’m having the same problem. Was this resolved? Thanks.

I have the same problem. I update an image in the documents directory, but since the image file does not change, I think Corona assumes the file is the same.

I’m not really sure what the problem is but you can try this: If you are getting image remotely and have to 100% sure you are getting a fresh version and not cached by either db or device you can append a unique string to the url, like a date.

[lua]“http://www.google.com/logo.jpg?currentTime=” … os.time()[/lua]

I am loading local files. I have a picture editor, and I save the file as “myfile.jpg”. When I try to load the updated version, I only see the first-loaded version due to caching.

I found a work-around: rename the file, load it, then rename it back to the proper name. Works just fine. The cached image will eventually be deleted if memory is needed, so it seems safe.

I have the same problem. I update an image in the documents directory, but since the image file does not change, I think Corona assumes the file is the same.

I’m not really sure what the problem is but you can try this: If you are getting image remotely and have to 100% sure you are getting a fresh version and not cached by either db or device you can append a unique string to the url, like a date.

[lua]“http://www.google.com/logo.jpg?currentTime=” … os.time()[/lua]

I am loading local files. I have a picture editor, and I save the file as “myfile.jpg”. When I try to load the updated version, I only see the first-loaded version due to caching.

I found a work-around: rename the file, load it, then rename it back to the proper name. Works just fine. The cached image will eventually be deleted if memory is needed, so it seems safe.