Type Function Library display.* Return value none Revision 2017.3060 Keywords networking, downloading, image download, remote image See also network.download() network.request()
This a convenience method, similar to network.download(), which returns a display object containing the image, as well as saving the image to a file.
display.loadRemoteImage( url, method, listener [, params], destFilename [, baseDir] [, x, y] )
String. The HTTP request URL which should point to a valid remote PNG or JPG file.
String. The HTTP method; acceptable values are "GET"
(default) or "POST"
.
Listener. The listener function invoked when the HTTP operation has completed. It is passed an event
object that contains the following properties:
event.response
— a string containing the destination file name. This is useful if you're writing a general event handler for a variety of file downloads.event.target
— the new display object created after the image is downloaded.event.isError
— a Boolean value: true
in the case of a network error; false
if not.Table. See the Parameters section for more information.
String. The name of the file to which the HTTP response will be saved.
Constant. The folder path to which the file will be saved.
Numbers. The x and y coordinates to place the newly created display object, once the file has been successfully downloaded and saved.
The params
table is an optional table that specifies custom HTTP headers or body to include in the request. To specify custom headers, attach a headers
table that specifies header values with string keys. To specify a custom body message, attach a body
property to this table whose string value is the HTTP body.
local params = {} params.headers = {} -- contains header values to send with your request params.body = "" -- specific body to send with your request
Table. A table specifying header values with string keys.
String. Contains the HTTP body to send with your request.
Nothing is returned from calling this function. The listener callback will contain the display object value (event.target
) if the download was successful.
local function networkListener( event ) if ( event.isError ) then print ( "Network error - download failed" ) else event.target.alpha = 0 transition.to( event.target, { alpha = 1.0 } ) end print ( "event.response.fullPath: ", event.response.fullPath ) print ( "event.response.filename: ", event.response.filename ) print ( "event.response.baseDirectory: ", event.response.baseDirectory ) end display.loadRemoteImage( "http://coronalabs.com/images/coronalogogrey.png", "GET", networkListener, "coronalogogrey.png", system.TemporaryDirectory, 50, 50 )
--The 'display.loadRemoteImage()' API call is a convenience method around the 'network.request()' API. --The code below is the implementation of 'display.loadRemoteImage()'. If you need to cancel your call, --feel free to use the code below and modify it to your needs. local function networkListener( event ) if ( event.isError ) then print ( "Network error - download failed" ) else event.target.alpha = 0 transition.to( event.target, { alpha = 1.0 } ) end print ( "event.response.fullPath: ", event.response.fullPath ) print ( "event.response.filename: ", event.response.filename ) print ( "event.response.baseDirectory: ", event.response.baseDirectory ) end local function remoteImageListener( self, event ) local listener = self.listener local target if ( not event.isError and event.phase == "ended" ) then target = display.newImage( self.filename, self.baseDir, self.x, self.y ) event.target = target end listener( event ) end -- display.loadRemoteImage( url, method, listener [, params], destFilename [, baseDir] [, x, y] ) display.loadRemoteImage = function( url, method, listener, ... ) local arg = { ... } local params, destFilename, baseDir, x, y local nextArg = 1 if ( "table" == type( arg[nextArg] ) ) then params = arg[nextArg] nextArg = nextArg + 1 end if ( "string" == type( arg[nextArg] ) ) then destFilename = arg[nextArg] nextArg = nextArg + 1 end if ( "userdata" == type( arg[nextArg] ) ) then baseDir = arg[nextArg] nextArg = nextArg + 1 else baseDir = system.DocumentsDirectory end if ( "number" == type( arg[nextArg] ) and "number" == type( arg[nextArg + 1] ) ) then x = arg[nextArg] y = arg[nextArg + 1] end if ( destFilename ) then local options = { x=x, y=y, filename=destFilename, baseDir=baseDir, networkRequest=remoteImageListener, listener=listener } if ( params ) then network.download( url, method, options, params, destFilename, baseDir ) else network.download( url, method, options, destFilename, baseDir ) end else print( "ERROR: no destination filename supplied to display.loadRemoteImage()" ) end end display.loadRemoteImage( "http://coronalabs.com/images/coronalogogrey.png", "GET", networkListener, "coronalogogrey.png", system.TemporaryDirectory, display.contentCenterX, display.contentCenterY )