display.newImage() Stop Scaling Large Images

After Googling, I think found out _ how _ to stop an image from scaling that is wider than the display:

-- note the boolean on the end... local wideImage = display.newImage('images/wideImage.png', true)

However, after reading the documentation, I could not find this option, and cannot figure out _ why _ this works:
https://docs.coronalabs.com/api/library/display/newImage.html

I can live with the fact that this works, but being new to Corona, I’d like pointers on how I can identify these options in the future.

Many thanks in advance!

I don’t know what you’re doing there, but that is wrong.  

If it behaves the way you want it to, somehow, it is purely luck.  

Please provide a link to the article or code that shows this as a valid technique, which it is not.

If you want a display.newImage() object to NOT scale beyond the width and height of the screen, you have at least two options:

  1. Use display.newImageRect() and specify the width and height yourself.

    local tmp = display.newImageRect( ‘images/wideImage.png’, display.actualContentWidth, display.actualContentHeight ) tmp.x = display.contentCenterX tmp.y = display.contentCenterY

  2. Scale it after you make it.

    local tmp = display.newImage( ‘images/wideImage.png’, display.contentCenterX, display.contentCenterY ) tmp:scale( display.actualContentWidth/tmp.width, display.actualContentHeight/tmp.height)

Suggestions:

  • Never rely on side-effects or random undocumented behaviors that ‘seem to work’.  
  • Always go with the defined functionality or you will eventually find yourself very sad and frustrated trying to debug your app/game.

There used to be an optional boolean argument for display.newRect that defaulted to false, and unless you set it to true, oversized images would be scaled to (IIRC) the device’s screen resolution. It may still be active if undocumented.

I still add a ‘true’ final argument to my large display.newImage objects out of habit, just to make sure they are rendered at their native resolution. I’m also a habitual user of the xScale & yScale properties to resize my images as needed. (I personally never use the :scale() method because it’s always relative to the object’s current size, whereas xScale & yScale are absolute.)

But in general, using display.newImageRect will give you greater control and confidence that you know exactly what you’re going to see - and it will have the bonus effect of being ready for @2x/@3x/etc assets if you use them.

Ah that makes sense.

It seemed like I was using a “leftover” property which did not smell good in my code, so I feel that little mystery is solved now.

I was also able to find what you were talking about with regard to xScale and yScale just fine in the docs.

display.newImageRect() is what I needed to get the job done reliably, thanks!

Personally I always use display.newImageRect() unless I’m loading an image that I don’t know the size of. Any thing I create for the game/app I will know the size of. If I download some image from the Internet I’m not likely going to know the width and height of it. Then I will use display.newImage() and scale it.  

Rob

I don’t know what you’re doing there, but that is wrong.  

If it behaves the way you want it to, somehow, it is purely luck.  

Please provide a link to the article or code that shows this as a valid technique, which it is not.

If you want a display.newImage() object to NOT scale beyond the width and height of the screen, you have at least two options:

  1. Use display.newImageRect() and specify the width and height yourself.

    local tmp = display.newImageRect( ‘images/wideImage.png’, display.actualContentWidth, display.actualContentHeight ) tmp.x = display.contentCenterX tmp.y = display.contentCenterY

  2. Scale it after you make it.

    local tmp = display.newImage( ‘images/wideImage.png’, display.contentCenterX, display.contentCenterY ) tmp:scale( display.actualContentWidth/tmp.width, display.actualContentHeight/tmp.height)

Suggestions:

  • Never rely on side-effects or random undocumented behaviors that ‘seem to work’.  
  • Always go with the defined functionality or you will eventually find yourself very sad and frustrated trying to debug your app/game.

There used to be an optional boolean argument for display.newRect that defaulted to false, and unless you set it to true, oversized images would be scaled to (IIRC) the device’s screen resolution. It may still be active if undocumented.

I still add a ‘true’ final argument to my large display.newImage objects out of habit, just to make sure they are rendered at their native resolution. I’m also a habitual user of the xScale & yScale properties to resize my images as needed. (I personally never use the :scale() method because it’s always relative to the object’s current size, whereas xScale & yScale are absolute.)

But in general, using display.newImageRect will give you greater control and confidence that you know exactly what you’re going to see - and it will have the bonus effect of being ready for @2x/@3x/etc assets if you use them.

Ah that makes sense.

It seemed like I was using a “leftover” property which did not smell good in my code, so I feel that little mystery is solved now.

I was also able to find what you were talking about with regard to xScale and yScale just fine in the docs.

display.newImageRect() is what I needed to get the job done reliably, thanks!

Personally I always use display.newImageRect() unless I’m loading an image that I don’t know the size of. Any thing I create for the game/app I will know the size of. If I download some image from the Internet I’m not likely going to know the width and height of it. Then I will use display.newImage() and scale it.  

Rob