How do I remove remote images?

I’ve loaded a few images using this:

[lua]display.loadRemoteImage( “http://developer.anscamobile.com/demo/hello.png”, “GET”, networkListener, “helloCopy.png”, system.TemporaryDirectory, 50, 50 )[/lua]

And I want to have a button that removes them all. How should I use the removeSelf function (or any other way) to do it? [import]uid: 98287 topic_id: 28991 reply_id: 328991[/import]

Never used remote images - but couldn’t you assign them to a local variable and then use removeSelf() on that?

Something like:

  
local image = display.loadRemoteImage( "http://developer.anscamobile.com/demo/hello.png", "GET", networkListener, "helloCopy.png", system.TemporaryDirectory, 50, 50 )  
  
image:removeSelf()  
  

I might be completely wrong but that’s how I’d go about it. [import]uid: 33275 topic_id: 28991 reply_id: 116677[/import]

That’s what I tried, but it doesn’t work. I tried moving the images too but no luck there either.

I like your advice to never use remote images, but I’m afraid I’ve painted myself into a corner :smiley: [import]uid: 98287 topic_id: 28991 reply_id: 116678[/import]

I posted a reply and then checked the documentation - seems this is the desired way that loadRemoteImage works - doh!

“Nothing is returned from the calling function…”

It seems the listener function on the callback will have the image in event.target - you could be able to store that in a local variable and away you go:

Something like:

local img  
  
function networkListener()  
 if event.target then  
 img = event.target  
 end  
end  
  
img:removeSelf()  

Hopefully that works - haven’t tested it, but it should point you in the right direction.
[import]uid: 33275 topic_id: 28991 reply_id: 116685[/import]

I’m sorry, I’m not a professional programmer, so I kinda understand what you’re saying, but it’s a bit foggy.

Could you show me where in my code to add what you just posted?

-- IMG  
  
local image = display.loadRemoteImage( "http://developer.anscamobile.com/demo/hello.png", "GET", networkListener, "helloCopy.png", system.TemporaryDirectory, 50, 50 )  
-- CLOSE  
  
 local close = display.newRect(300, 200, 200, 200)  
 close.alpha = 0.5  
 --  
 function close:tap( event )  
 image:removeSelf()  
 return true  
 end  
   
 close:addEventListener( "tap", close )  

[import]uid: 98287 topic_id: 28991 reply_id: 116692[/import]

No worries.

Your call to display.loadRemoteImage won’t return anything.

So in that call you’ve defiend a listener/callback function, when your request comes back it’s directed to that function. In your code this is defined as networkListener - the third parameter in your call.

So you need a function like this that will handle the callback:

local img  
  
local close = display.newRect(300, 200, 200, 200)  
 close.alpha = 0.5  
  
function networkListener(event)  
 -- in here we handle the event and that's where we get the image.  
 if event.target then  
 img = event.target  
 end  
end  
  
function close:tap( event )  
 if img then  
 img:removeSelf()  
 end  
 return true  
end  
   
close:addEventListener( "tap", close )  
  
display.loadRemoteImage( "http://developer.anscamobile.com/demo/hello.png", "GET", networkListener, "helloCopy.png", system.TemporaryDirectory, 50, 50 )  

That should get an image onscreen and remove it when you click the close button.

The example on the documentation page is pretty clear and concise, should help you - but if you need any further tips let me know.

http://docs.coronalabs.com/api/library/display/loadRemoteImage.html [import]uid: 33275 topic_id: 28991 reply_id: 116694[/import]

Yes! That works, thanks SegaBoy!
Now I’ll see if I understand enough to make the button close all four images.
If I figure it out before you get back here I’ll post it, but if you get here before that and have the patience to point me in the right way, I’ll appreciate it. [import]uid: 98287 topic_id: 28991 reply_id: 116863[/import]

Cool cool, think it’s probably a good idea if you have a crack at it first - helps to learn that way. If you struggle I’ll keep an eye on this thread and give you a hand.
[import]uid: 33275 topic_id: 28991 reply_id: 116871[/import]

A-HA! I see your one disappearing image and raise you three more:

-- CLOSE  
  
 local close = display.newRect(20, 380, 200, 200)  
 close:setFillColor (255, 255, 255)  
 close.alpha = 1  
  
 local img  
  
 function networkListener1(event)  
 if event.target then  
 img1 = event.target  
 end  
 end  
--  
 function networkListener2(event)  
 if event.target then  
 img2 = event.target  
 end  
 end  
--  
 function networkListener3(event)  
 if event.target then  
 img3 = event.target  
 end  
 end  
--  
 function networkListener4(event)  
 if event.target then  
 img4 = event.target  
 end  
 end  
  
   
 function close:tap( event )  
 if img1 then  
 img1:removeSelf()  
 end  
 if img2 then  
 img2:removeSelf()  
 end  
 if img3 then  
 img3:removeSelf()  
 end  
 if img4 then  
 img4:removeSelf()  
 end  
 return true  
 end  
  
close:addEventListener( "tap", close )  
  
-- REMOTE IMAGES  
  
display.loadRemoteImage( "http://www.light-sleepers.com/otherbooks/book1.png", "GET", networkListener1, "book1.png", system.TemporaryDirectory, 250, 50)  
  
display.loadRemoteImage( "http://www.light-sleepers.com/otherbooks/book2.png", "GET", networkListener2, "book2.png", system.TemporaryDirectory, 250, 270)  
  
display.loadRemoteImage( "http://www.light-sleepers.com/otherbooks/book3.png", "GET", networkListener3, "book3.png", system.TemporaryDirectory, 250, 490)  
  
display.loadRemoteImage( "http://www.light-sleepers.com/otherbooks/book4.png", "GET", networkListener4, "book4.png", system.TemporaryDirectory, 250, 710)  

Let me know if there’s something redundant in there.
[import]uid: 98287 topic_id: 28991 reply_id: 117157[/import]

Cool cool - if it works it works - nice one.

If you want a further challenge you can definitely tighten up that code a little; for starters you could have a function responsible for loading the remote images and passing in the path and destination filename as parameters.

Equally you don’t need to have all those networkListeners - once would suffice with a little out the box thinking. I would recommend looking at storing all of the remote images in a table and using their filename as the index - provided you can get that from the event.

You’re also forward declaring img as a local but not using it - img1, img2, img3, img4 will all be globals (boo, hiss) :slight_smile: Quick solution is to declare img1,img2,img3,img4 as locals where local img is defined.

Some of these suggestions may be a bit OTT - it’s mainly to make the code neater and easier to read and help you to write tighter code, I don’t think any of the suggestions will improve performance, etc… It all depends on how neat you want your code to be.

But like I said nice one, it’s definitely the best way to learn things by just trying with a few pointers.

[import]uid: 33275 topic_id: 28991 reply_id: 117158[/import]

Thanks, I was hoping for just that kind of clean-up tips. It’s a funny feeling when your code works, but somehow doesn’t look like professional code :slight_smile: [import]uid: 98287 topic_id: 28991 reply_id: 117159[/import]

I’ve rewritten the function the way in which I think it can be tidied up, haven’t tested it and it may not work the way you intended.

If you struggle tidying up things, give me a shout and I’ll show you what I’ve done. [import]uid: 33275 topic_id: 28991 reply_id: 117161[/import]

Ok I’m going to pick the easy way out here, since I have a bunch of other stuff to set up on that page (for which I’ll probably need help too), so if you’ll please show me the cleaned-up version I’d appreciate it :slight_smile: [import]uid: 98287 topic_id: 28991 reply_id: 117179[/import]