An Option to Disable display.newImage() File-Caching

Hello!

I’m using display.save to create thumbnails, and since the user continually makes changes which requires updated thumbnails, the image content of those thumbnail image files change over the course of the app’s life.

When listing the thumbnails using display.newImage, i want to show the latest thumbnail, but since newImage caches images based on file-path, it keeps loading the old image data, causing the thumbnails to look outdated.

The suggestion in the doc is to always write to new file-names, but this does not sound like a very good solution and does not scale well with an unknown amount of changes to thumbnails.

I would like a way to avoid using the image cache when loading the thumbnail images.

A couple of ideas for how to achieve this:

  • Another argument in the newImage function to control caching, defaulting to true.
  • A separate function call to invalidate the image cache at a certain path, allowing the next call to read fresh.
  • A method on the imageobject, forcing a re-read from already configred path and folder.
  • A new display create method which is less clever and always reads from disk.
  • Implement file-watching in corona which detects updated files on disk in the project sandbox, and matches those paths to cached images and invalidates them automatically.

Thoughts?

Thanks!

Hello and welcome to the community!

Some of us have had to deal with this in the past, and your suggestions are really good. I would vote for an option to force a reload of the image from disk. :slight_smile:

In the meantime, a workaround is to remove the object/s that are currently using the image/texture before re/creating the object.

Textures are cached for the duration of their usage. When the last object using a texture is removed then said texture would be cleared from cache on the next frame cycle; important to note that next frame time window.

Thus, you can try first removing the object, if it already exists, and then create it with the same image file:

if thumbnail then thumbnail:removeSelf(); thumbnail = nil end
timer.performWithDelay(1, function() 
  thumbnail = display.newImage("screenshot.png")
  -- do other stuff with the thumbnail
end)