Keep aspect ratio when loading image with display.newImageRect

>> And the most important is that I don’t want to know it and specify its size in the code because it can change in the future.

Catch-22.  This is the source of your “problem”, which will persist in one form or another for as long as you force yourself to wear blinders.

In other words (as i said, as rob said) you (seem to) have access to all the information you need, but just refuse to use it, instead preferring to load images at run-time to rediscover that info, then ‘wishing’ there were some way you didn’t have to do so.

So why not build an “asset catalog” (you could even write a bit of lua as a dev-time utility to scan your files and produce it for you if you wanted), something like (just for ideas):

local assetCatalog = { ["image1.png"] = { w=210, h=190 }, ["image2.png"] = { w=100, h=100 }, -- etc } local cat = assetCatalog["image1.png"] print(cat.w, cat.h)

Then you could “look up” image dimensions as you wish.  Sure, you’d have to regenerate your catalog if/when your assets change, but that’s a small price to pay at development-time to avoid a bigger price at run-time.

The same reflexion can be applied to a lot of features, like the “Dynamic Image Selection” feature itself:

I could live without it. I could write a piece of code which compute the scale factor, choose an imageSuffix like “@3x”, and then when loading each image, add some code to append “@3x” and the end of the filename, try to load the @3x image, and if it doesn’t exist try to load @2x

I could totally do that. But fortunately Corona has this great feature and handles this for me, which is very convenient for a lot of people.

For me this is the same reflexion than my “problem”. Of course I could do like you said, make a tool which compute the size of each of my images…

But it would so more convenient to be able to retrieve the image size at runtime (without having to actually load it) or to have a parameter in display.newImageRect to keep the aspect ratio.

It doesn’t seem to be like a “crazy” or a “stupid” feature to me. Personally, it would be very useful and convenient to me.

So the whole purpose of my post was to ask if I have missed any feature like that and/or if you planned to add it.

And I have my answer now.

Thanks.

>>retrieve the image size at runtime (without having to actually load it) 

Just pretend you’re a Corona engineer for a moment and ask yourself how you’d do that.

Didn’t mean to imply your request was “crazy/stupid”, just that there are real ways around your problem right now, without having to wait for a feature request that may never come

>> Just pretend you’re a Corona engineer for a moment and ask yourself how you’d do that.

By just loading the header of the image file and accessing only this info. It wouldn’t be as heavy as loading the whole texture into video memory like display.newImage does I suppose.

Btw, I found a post with Lua implementation to access png and jpg info (size and some other useful metadata) without using display.newImage. Could be an interesting alternative:

http://developer.coronalabs.com/forum/2012/05/16/pnglib-extract-data-png-files-width-height-color-depth-etc

Thanks

no worries, we just “think differently” if you think that approach is “easier”, and that’s fine

if you DO feature request something, i’d separate it out into a “please provide a ‘get image header info’ method” request, that you could then use to calc/pass values to newImageRect, rather than an internal modification to how/what newImageRect does ‘automagically’ – guessing that it’d be more likely to get the proper consideration if worded that way

Thanks for the tip :wink:

In fairness, I have an app that displays photos. The photos can vary in size and I use display.newImage() at run time to load the image and then I fit them to a box. But there are not @2x and @4x versions. As I said above, if you have @2x images then you probably made the the two images and therefore you have the image size and can program them in.

But if you’re needing images you can’t control, you can use this scaling code.

local image = display.newImage("Image.png") local s if image.width \> 100 then -- 100 is the box width     s = 100 / image.width     image:scale( s, s ) end if image.contentHeight \> 100 then -- check to see if we are still too high. Need to use the scaled height though.    s = 100 / image.height    image:scale( s, s ) end

or something like that.

Rob