OK, here’s another way to handle this problem. This is the main code for caching image file sizes. Each time you call getImageSize, it checks whether the image is in the list, ImageInfoList. That list of sizes is stored as a JSON file in the caches directory. So, as the images are loaded, the sizes are stored, meaning the next time the image is loaded the sizes are already figured out.
It seems to speed up the loading a lot.
Just make sure the table, ImageInfoList, is accessible! Note, it is declared just before the getImageSize() function. You might want it elsewhere.
[code]
– Save/load functions
function saveData(filePath, text)
–local levelseq = table.concat( levelArray, “-” )
local file = io.open( filePath, “w” )
if (file) then
file:write( text )
io.close( file )
return true
else
print ("Error: funx.saveData: Could not create file "…tostring(filePath))
return false
end
end
function loadData(filePath)
local t = nil
–local levelseq = table.concat( levelArray, “-” )
local file = io.open( filePath, “r” )
if (file) then
t = file:read( “*a” )
io.close( file )
else
print ("funx.loadData: No file found at "…tostring(filePath))
end
return t
end
function saveTableFromFile(filePath, dataTable)
–local levelseq = table.concat( levelArray, “-” )
file = io.open( filePath, “w” )
for k,v in pairs( dataTable ) do
file:write( k … “=” … v … “,” )
end
io.close( file )
end
function loadTableFromFile(filePath)
local file = io.open( filePath, “r” )
if file then
– Read file contents into a string
local dataStr = file:read( “*a” )
– Break string into separate variables and construct new table from resulting data
local datavars = split(dataStr, “,”)
dataTableNew = {}
for i = 1, #datavars do
– split each name/value pair
local onevalue = split(datavars[i], “=”)
dataTableNew[onevalue[1]] = onevalue[2]
end
io.close( file ) – important!
– Note: all values arrive as strings; cast to numbers where numbers are expected
dataTableNew[“numValue”] = tonumber(dataTableNew[“numValue”])
dataTableNew[“randomValue”] = tonumber(dataTableNew[“randomValue”])
else
print (“no file found”)
end
end
function saveTable(t, filename, path)
if (not t or not filename) then
return true
end
path = path or system.DocumentsDirectory
–print ("funx.saveTable: save to "…filename)
local json = json.encode (t)
local filePath = system.pathForFile( filename, path )
saveData(filePath, json)
end
function loadTable(filename, path)
path = path or system.DocumentsDirectory
local filePath = system.pathForFile( filename, path )
–print ("funx.loadTable: load from "…filePath)
local t = {}
local f = loadData(filePath)
if (f and f ~= “”) then
t = json.decode(f)
end
–print (“loadTable: end”)
return t
end
– Image loading
function getScaledFilename(filename, d)
local scalingRatio = scaleFactorForRetina()
local scalesuffix = “@”…scalingRatio…“x”
– Is there an other sized version?
local suffix = string.sub(filename, string.len(filename)-3, -1)
local name = string.sub(filename, 1, string.len(filename)-4)
local f2 = name … scalesuffix … suffix
– If no scaled file, get original
if (fileExists(f2,d)) then
filename = f2
return filename, scalingRatio
else
return filename, 1
end
end
– Get an image size (height, width)
– We need this to use it for display.newImageRect
– Let’s keep a list of image sizes so we can avoid double-loading.align
– Every time we load, save the size to a list we maintain.
local ImageInfoList = loadTable(“images_info.json”, system.CachesDirectory) or {}
function getImageSize(f,d)
d = d or system.ResourceDirectory
– load info table, if not loaded
if (not ImageInfoList) then
ImageInfoList = loadTable(“images_info.json”, system.CachesDirectory)
end
– Check the sizes list for this image
if (ImageInfoList[f]) then
return ImageInfoList[f].width, ImageInfoList[f].height
else
– add to the list
local i = display.newImage(f,d,true)
local w = i.contentWidth
local h = i.contentHeight
i:removeSelf()
i = nil
ImageInfoList[f] = { width = w, height = h }
saveTable(ImageInfoList, “images_info.json”, system.CachesDirectory)
return w,h
end
end
[/code] [import]uid: 37366 topic_id: 22353 reply_id: 101709[/import]