Getting the dimensions of an image


We use a script that allows us to create a simple sprite animation by only accepting the path of a file, frame size and time per frame. Essentially what we do is to load the entire PNG off screen and then fetch its size using .width and .height attributes, perform basic math to determine the number of frames in the sheet and then use the Solar2D API methods to generate and return an animation object.

This works well most of the time but recently we noticed that on smaller device screens like iphone6 etc, the values returned by for the width and height of the PNG file were clamped at 1024. I have checked that our images are well within the maxTextureSize on these devices and cannot quite work out the reason for this.

For all our projects, we use Letterbox scaling so I tried changing that to zoomEven and the other scaling types but the width and height are still incorrectly reported.

Basically, we’re looking for a simple way to get the actual dimensions of a PNG file stored at a location in the resourceDirectory-- any help will be appreciated.


This does appear to be one of the “gotchas” in the newImage docs:

Seems odd that it would be doing this if you have confirmed that the images are smaller than the maxTextureSize though.

Yes, I saw this and did find it quite odd. I can also confirm that this is happening only on Apple devices.

I wonder if this is how it’s intended in the engine and what the rationale might be because the same devices seem perfectly capable of displaying the full image if the dimensions are set explicitly in the code

@vlads sorry for the tag. This is probably not a critical bug or anything but can you please confirm that this behaviour is expected?

Right so earlier I had thought, I would just go ahead and explicitly provide dimensions of the display objects and imageSheets that were causing problems and that’ll fix it for the time being but there are further issues.

When I make an image sheet, I usually provide the function call with a table of all the individual frames’ data so as per the API, the contentWidth and contentHeight of the sheet are optional. With this process, the sheet is still clamped to 1024x1024 so then I resorted to also adding in the optional parameters to specify the width and height of the sheet but that leads to a new problem-- the animations are wobbly on some devices yet perfectly fine and stable on others. I am including a video that shows a menu with an animated grid of buttons and in the iphoneX simulator, it runs perfectly but is very unstable on the iphone6 skin.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.