display.loadRemoteImage( )

Description:

This a convenience method, similar to network.download(), which returns a display object containing the image, as well as saving the image to file.

Syntax:

object = display.loadRemoteImage( url, method, listener [, params], destFilename [, baseDir] [, x, y] )

Example:


The following example downloads a remote image to a local file copy, and then displays it on the screen at (50, 50) after a short alpha transition:

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 ( "RESPONSE: " .. event.response )
end
 
display.loadRemoteImage( "http://developer.anscamobile.com/demo/hello.png", "GET", networkListener, "helloCopy.png", system.TemporaryDirectory, 50, 50 )

Parameters:

url
String: The HTTP request URL.

method
String: The HTTP method; valid values are "GET" (the default) or "POST".

listener
Function: 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 filename. 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 otherwise.

params
Table: 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.

  • params.headers
  • A table specifying header values with string keys.

  • params.body
  • A string containing the HTTP body.

destFilename
String: The name of the file to which the HTTP response will be saved.

baseDir
String: The folder path to which the file will be saved.

x
The x position of the newly created display object; default is 0.

y
The y position of the newly created display object; default is 0.

Returns:

Nothing is returned from calling this method. The listener callback will contain the Display Object value (event.target) if the download was successful.

Remarks:

Image Guidelines
Images should not contain an embedded ICC profile. Use "Save for Web" when exporting images to be used on the device.

To avoid images from using too much texture memory, make sure the image is between 72 and 170 DPI.

Be aware of the gamma and color differences between your development system (Mac and Windows machines) and the device. For best results color calibrate the display used for creating the images.

Supported on operating systems and platforms for build numbers shown:
  • Mac OS X:
    Build 2011.268
  • Windows:
    --
  • iOS:
    Build 2011.268
  • Android:
    Build 2011.336

Replies

JayantV
User offline. Last seen 20 weeks 6 days ago. Offline
Joined: 31 Oct 2009

Just a quick question, if loadRemoteImage is downloading an image data from a network, why can FTP not be used?

Tom
User offline. Last seen 4 years 3 weeks ago. Offline
Joined: 13 Jul 2010

You can use FTP to download the image. This API is more of a convenience feature that calls the network.download function.

i.callaghan8
User offline. Last seen 9 years 22 weeks ago. Offline
Joined: 1 Mar 2011

can this be used to download & display an HTML text document

mat_horn
User offline. Last seen 9 years 19 weeks ago. Offline
Joined: 19 Jan 2011

Why does this code not work on iPhone 4 ?

Has anyone an advice??

jamesguitar3
User offline. Last seen 9 years 18 weeks ago. Offline
Joined: 20 Jan 2011

How can I remove the the displayed image from the screen?
Thank you.

gbailey
User offline. Last seen 4 years 35 weeks ago. Offline
Joined: 26 May 2011

I tried this code, and it fails. At first I thought perhaps because he had to be running on the device, but it fails even when I do that.

I get the following error, any ideas?

Runtime error
Invalid comment: /Users/dongeraldo/Library/Application Support/Corona Simulator/3-54D416DB710B575C12E83CF5784960C2/tmp/helloCopy.png
stack traceback:
[C]: ?
[C]: in function '?'
?: in function 'ReadComment'
?: in function 'SkipWhiteSpace'
?: in function <?:269>
(tail call): ?
...796fl6g3yw_yh9brw40000gp/T/TemporaryItems/3/main.lua:79: in function <...796fl6g3yw_yh9brw40000gp/T/TemporaryItems/3/main.lua:74>
(tail call): ?
?: in function <?:718>

selim.anac
User offline. Last seen 8 years 14 weeks ago. Offline
Joined: 1 Aug 2011

Is there a way to cancel this operation? I mean what if the user changes the scene before download completed?

doga.tuncyuz
User offline. Last seen 8 years 40 weeks ago. Offline
Joined: 19 Dec 2011

I'm trying to load the image from a remote host without saving it locally. Is it possible not to save the file?

gbailey
User offline. Last seen 4 years 35 weeks ago. Offline
Joined: 26 May 2011

My understanding is that saving the file is optional. Have you tried it using the example above with and without saving it?

Johantd04
User offline. Last seen 3 years 4 weeks ago. Offline
Joined: 22 Nov 2011

What if the images already was downloaded? Will it load the image from the filesystem or will it download the image again?

open768
User offline. Last seen 1 year 3 weeks ago. Offline
Joined: 6 Jul 2011

hmm, 2nd time I call this function it craps out silently. I dont get why it doesnt call the callback with an error also so it can be reacted to. I suspect it doesnt succeed if the destination file allread exists.

gbailey
User offline. Last seen 4 years 35 weeks ago. Offline
Joined: 26 May 2011

Does anybody know how to scale a remote image? I've done this to get the image:

1
day1WeatherIcon = display.loadRemoteImage(day1WeatherIconURL, "GET", self, "Day 1 Weather Icon", system.TemporaryDirectory, 25, screenHeight-50)

I've then tried both day1WeatherIcon:scale(.5, .5) as well as day1WeatherIcon.xScale = .5 and get an error each time.

Using build 767. Any ideas? Thanks!

Skatan
User offline. Last seen 4 years 39 weeks ago. Offline
Joined: 11 Jan 2011

gbailey, attach a listener and from inside the function scale it

1
2
3
4
5
local function listener(event)
    event.target.xScale = 0.5
    event.target.yScale = 0.5
end
display.loadRemoteImage(url, "GET", listener, "img.png", system.TemporaryDirectory)

jacques1
User offline. Last seen 21 weeks 6 hours ago. Offline
Joined: 2 Jun 2011

I just have a quick question. The below code works fine in the Xcode simulator, however when i test on my device only the first image is shown, almost as if it hangs and doesn't continue the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local assetPath = "assets/"
 
local halfW = display.viewableContentWidth / 2
local halfH = display.viewableContentHeight / 2
 
local pix={}
local pixGroup=display.newGroup();pixGroup.x=100;pixGroup.y=100
 
local txt = display.newText("", 0, 0, "Feast of Flesh BB", 45)
pixGroup:insert(txt,true)
txt.text="IMAGES"
txt:setTextColor( 255, 255, 200, 255)
txt.x=(halfW*2-350);txt.y=halfH-85
 
local function showImage( event )
        event.target.alpha = 0
        event.target.xScale = 1
        event.target.yScale =1
end
                                                        
for i=1,#data do
 
display.loadRemoteImage("http://graph.facebook.com/".. data[i].id .."/picture",
        "GET",
        showImage,
        "friend"..i..".png",
        system.TemporaryDirectory,
        400,
        i*10 )
 
pix[i]=display.newGroup();pixGroup:insert(pix[i])
pix[i].x=pixGroup[1].x;pix[i].y=halfH-200;pix[i].alpha=1
pix[i].xScale=1;pix[i].yScale=1
local img=display.newImage("friend"..i..".png", system.TemporaryDirectory,0,0)
pix[i]:insert(img,true)
 
local img=display.newRect(0,0,pix[i][1].width,pix[i][1].height)
pix[i]:insert(img,true)
img:setFillColor( 150, 0, 0 )
img.blendMode = "add"
 
local img=graphics.newMask(assetPath .. "pix_mask.png")  
pix[i]:setMask(img)
pix[i].maskX=0
pix[i].maskY=0
 
end

If i remove my attempt at putting the loaded images into a display group then the images all appear, however i require the downloaded images to be put into a display group for further use.

Tom
User offline. Last seen 4 years 3 weeks ago. Offline
Joined: 13 Jul 2010

Please move questions to the forums.

Thanks.