-
Use display groups for layering: https://docs.coronalabs.com/api/library/display/newGroup.html
-
Using scaling to ‘resize’: https://docs.coronalabs.com/api/type/DisplayObject/scale.html
-
When I download your image, it is “INVALID”. Something is either wrong with the image or your server.
Where do I put the URL? This doesn’t make sense. Is this for a background image?
This demo shows you how to do it:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/10/remoteBackImage.zip
io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ===================================================== local cx = display.contentCenterX local cy = display.contentCenterY local fullw = display.actualContentWidth local fullh = display.actualContentHeight local left = cx - fullw/2 local right = cx + fullw/2 local top = cy - fullh/2 local bottom = cy + fullh/2 -- Display groups for easy layering of content local backgroundGroup = display.newGroup() local contentGroup = display.newGroup() -- Create an initial backround image so you can see what is happening local initialBack = display.newImageRect( backgroundGroup, "protoBackX.png", 720, 1386 ) initialBack.x = cx initialBack.y = cy if( display.contentWidth \> display.contentHeight ) then initialBack.rotation = 90 end -- Add some other content in our 'contentGroup' local tmp1 = display.newImageRect( contentGroup, "rg256.png", 256,256 ) local tmp2 = display.newImageRect( contentGroup, "corona256.png", 256,256 ) tmp1.x = cx tmp1.y = cy - 200 tmp2.x = cx tmp2.y = cy + 200 -- -- Code to download an image and make it the new background image -- -- YOUR URL IS INVALID?? USING MINE FOR DEMO --local imageURL = "http://www.mywebsite/image.png" local imageURL = "https://raw.githubusercontent.com/roaminggamer/RG\_FreeStuff/master/AskEd/2016/09/remoteFiles/images/cardClubs2.png" local allowStretching = false local function networkListener( event ) --table.print\_r(event) if ( event.isError ) then print ( "Network error - download failed" ) initialBack:setFillColor(1,0,0) else -- remove old background image display.remove( initialBack ) initialBack = nil -- Place new background image in proper group local newBackground = event.target backgroundGroup:insert( newBackground ) -- Scale it up to fully cover the screen local ratioW = fullw/newBackground.contentWidth local ratioH = fullh/newBackground.contentHeight if( allowStretching ) then newBackground:scale( ratioW, ratioH ) else if( ratioW \> ratioH ) then newBackground:scale( ratioW, ratioW ) else newBackground:scale( ratioH, ratioH ) end end end print ( "event.response.fullPath: ", event.response.fullPath ) print ( "event.response.filename: ", event.response.filename ) print ( "event.response.baseDirectory: ", event.response.baseDirectory ) end -- Delay this for 2 seconds for the purpose of slowing this demo down timer.performWithDelay( 2000, function() display.loadRemoteImage( imageURL, "GET", networkListener, "image.png", system.TemporaryDirectory, cx, cy ) end )
You put the URL where you have it in display.loadRemoteImage(). One you have the image loaded in your listener function, you can use the .toBack() API (http://docs.coronalabs.com/api/type/DisplayObject/toBack.html) to send the image to the back of the display group.
Now that said, depending on the success of your app and how often it runs, you may crush your server downloading the image over and over.
You might find it more practical to see if the image exists, then perhaps do a network.request() using the “HEAD” verb and check to see if the file is newer on the server as it is newer than the file you have before downloading it. You might want to consider using something beside display.loadRemoteImage() because of this.
Rob
If you’re going to be using a HTTP URL and if you intend to release for iOS, you need to set up ATS: https://docs.coronalabs.com/guide/hardware/appleATS/index.html