Get width and height of a remote image using

How to get the original width and height of an image that’s loaded remotely when using

Hey @bgmadclown, I got Attempt to index field ‘target’ (a nil value) Here’s the code:

local function networkListener(event)
      if (event.isError) then
        print ("Network error - download failed");
      elseif (event.phase == "ended") then
        print (,,;

local requestId =
      {progress = true},

Is that " intended to be near temp.png?

Sorry, that was just a typo while copying the code. I edited it to its current format.

Oh sorry, I missed the part. My bad! I don’t know how to get the width and height through but you may want to try display.loadRemoteImage() with destFilename. Pretty much works the same.

Yeah I was earlier loading the image using display.loadRemoteImage() but I needed an option to cancel the loading if the scene has changed so I’m currently using

In this topic you said what it worked

Yes yes, there was a bug which I figured out yesterday. (Just updated the topic) So as an alternative using

Have you tried using network.request() to download the image and network.cancel() when you want to stop the download? Does it work?

(For the other topic, again, your issue could be solved by calling network.cancel())

Yeah I could try network.request(). I guess here I’ll have the option to do network.cancel() and also get the
Also, just confirming there’s no option to cancel a request while using display.loadRemoteImage() right?

Well, implementation is shared on documentation but I don’t know how easy it would be to modify it. You may need to take a look. Here:

1 Like

Oh yes, I did check this out. Seemed quite complicated to get it done compared to other options.

By using network.request() we do not get the original width and height of the image.


You can’t get the width and height of a remote image. You have to create it locally or use an advanced technique to inspect the file itself (after downloading it).

As far as the latter, I know some community members worked out how to do this in some cases, but it was never something that got used normally.

Your best bet is to download the image, then create a display object using it with display.newImage(), grab the width and height of the new image, then delete it.

Tip: SSK has a helper function to do this:

misc.getImageSize = function ( path, basePath )
	basePath = basePath or system.ResourceDirectory	
	local tmp = display.newImage( path, basePath, 10000,10000 )
	if( not tmp ) then
		return 0, 0
	local sx = tmp.contentWidth
	local sy = tmp.contentHeight
	return sx,sy
1 Like