Optionally Downloading And Using @2X Or @4X Graphics

In order to conserve space and guarantee more initial downloads, has anyone been able to implement (and is it possible) for their app to download the more high resolution versions of their assets upon request, then use them when doing a display.newImageRect function? It also alleviates large binaries for users who don’t have Retina or higher resolutions that will never see the graphics. Even if it means a replacement function that will search for the @2x or @4x version in the system.DocumentsDirectory before failsafing over to the system.ResourceDirectory. One could even offer the higher resolution version of their assets as an in-App Purchase, for example, or an alternate set of graphics, i.e. custom skinning downloads.

Woohoo!  Thought it over last night and tested this out this morning, it works!

 

Unfortunately, I couldn’t come up with a way to do it all in the display.newImageRect() function, since it either takes one argument for the filename, or two arguments for the filename and path, followed by the dimensions.  So, I created a new function to handle it all.

local function displayImage( filename, width, height, xpos, ypos ) local object object = display.newImageRect( filename, system.DocumentsDirectory, width, height ) if object == nil then object = display.newImageRect( filename, width, height ) end if object and xpos and ypos then object.x, object.y = xpos, ypos end return object end

 

Also, I didn’t take into account the optional parentGroup argument in the function, as I’ve always added it in after the fact.  But, if it’s something you normally do with every image you load, you should modify the function as needed.  I’ve also added two more arguments for instant positioning of the image.

 

So, in summary, when you do this:

local spaceship = displayImage( "spaceship.png", 128, 128, display.contentCenterX, display.contentCenterY )

 

It will look for spaceship@4x.png, spaceship@2x.png, and spaceship.png in your Documents directory first (provided you implemented automatic dynamic resolution in your config.lua file), and if it can’t find any of those, it will resort to loading the image (@4x, @2x, then original) in your Resource directory.  It will then position the spaceship in the middle of the screen, then return the object it created to the variable spaceship.

 

Now, I know it might show an error in the console when it can’t the appropriate image file in the Documents folder, so maybe by doing an ‘if file exists’ test first would alleviate that, but I’m not sure if you have to test for all resolution files.

Woohoo!  Thought it over last night and tested this out this morning, it works!

 

Unfortunately, I couldn’t come up with a way to do it all in the display.newImageRect() function, since it either takes one argument for the filename, or two arguments for the filename and path, followed by the dimensions.  So, I created a new function to handle it all.

local function displayImage( filename, width, height, xpos, ypos ) local object object = display.newImageRect( filename, system.DocumentsDirectory, width, height ) if object == nil then object = display.newImageRect( filename, width, height ) end if object and xpos and ypos then object.x, object.y = xpos, ypos end return object end

 

Also, I didn’t take into account the optional parentGroup argument in the function, as I’ve always added it in after the fact.  But, if it’s something you normally do with every image you load, you should modify the function as needed.  I’ve also added two more arguments for instant positioning of the image.

 

So, in summary, when you do this:

local spaceship = displayImage( "spaceship.png", 128, 128, display.contentCenterX, display.contentCenterY )

 

It will look for spaceship@4x.png, spaceship@2x.png, and spaceship.png in your Documents directory first (provided you implemented automatic dynamic resolution in your config.lua file), and if it can’t find any of those, it will resort to loading the image (@4x, @2x, then original) in your Resource directory.  It will then position the spaceship in the middle of the screen, then return the object it created to the variable spaceship.

 

Now, I know it might show an error in the console when it can’t the appropriate image file in the Documents folder, so maybe by doing an ‘if file exists’ test first would alleviate that, but I’m not sure if you have to test for all resolution files.