Loading multiple images from the web?

Hey all!

I’ve got the sample working for loading *1* single image, statically named “hello.png” found here:
http://developer.anscamobile.com/content/simple-image-download
Problem is, I have a table containing all my image locations out on the web server.

How do I load multiple images? Like, how do I get around the statically naming of files? Is there a way to pass a variable to the LoadRemoteImage function that will tell it to look for a particular filename? THATS my problem. Since it’s expecting an event, I don’t know how to proceed.

Here’s my code (which presently only downloads the first image file returned to my table from my web service, see the comments in code)

  
--  
-- Project: LevelDownloadTest  
-- Description:   
--  
-- Version: 1.0  
-- Managed with http://CoronaProjectManager.com  
--  
-- Copyright 2011 Mario Roberti. All Rights Reserved.  
--   
remoteDirectory = "http://www.iswdev.com/stuff/battlemaps/mapthumbs/"  
gameBoard = display.newGroup()  
function split(pString, pPattern)  
 local Table = {} -- NOTE: use {n = 0} in Lua-5.0  
 local fpat = "(.-)" .. pPattern  
 local last\_end = 1  
 local s, e, cap = pString:find(fpat, 1)  
 while s do  
 if s ~= 1 or cap ~= "" then  
 table.insert(Table,cap)  
 end  
 last\_end = e+1  
 s, e, cap = pString:find(fpat, last\_end)  
 end  
 if last\_end \<= #pString then  
 cap = pString:sub(last\_end)  
 table.insert(Table, cap)  
 end  
 return Table  
end  
local function LoadRemoteImage( event )  
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 local tempImage = display.newImage( "helloCopy.png", system.TemporaryDirectory)  
 tempImage.alpha = 0  
 gameBoard:insert(tempImage)  
 transition.to( tempImage, { alpha = 1.0 } )  
 gameBoard:insert(tempImage,false)  
 tempImage:toBack()  
 tempImage.x = math.random(100,400)  
 tempImage.y = math.random(100,400)  
 end  
  
 print ( "RESPONSE: " .. event.response )  
 loadSuccessful = true  
end  
   
  
function LoadRemoteText( event )  
 -- OK so my PHP script returnsa CSV string.  
 -- We'll parse that into a table using the   
 -- above 'split' function. No problem!  
  
  
 -- list=split(string\_to\_split,pattern\_to\_match)  
 -- e.g.:  
  
 list=split(event.response,",")  
 -- OK the table 'list' contains  
 -- my file names.  
  
 -- Gives us:  
 -- traderstop3thumb.jpg  
 -- withoutpitthumb.jpg  
 -- mineentrancethumb.jpg  
 -- dreestonsurroundsthumb.jpg  
 -- stormwatchinnthumb.jpg  
  
 for i=1,#list do  
 print("list["..i.."] "..list[i])  
 local tempFile = remoteDirectory..list[i] -- Concatenate my directory and my filename  
 -- Do the call to load the  
 network.download(tempFile, "GET", LoadRemoteImage, "helloCopy.png", system.TemporaryDirectory )  
 end  
  
end  
   
network.request( remoteDirectory.."enumerateImages.php", "GET", LoadRemoteText )  

I certainly appreciate any help!! (And here I thought the PHP stuff would be the HARD part! :wink: )

Best,
Mario [import]uid: 11636 topic_id: 15724 reply_id: 315724[/import]

[code]local fileName = “image”
local value = 5

local s = “image” … value … “.png”

print (s) << will print “image5.png”
remoteDirectory = “http://www.iswdev.com/stuff/battlemaps/mapthumbs/

local c = remoteDirectory … s

print© << will print http://www.iswdev.com/stuff/battlemaps/mapthumbs/image5.png"
[/code]

this should get your brain going.

.c
[import]uid: 24 topic_id: 15724 reply_id: 58103[/import]

Just from the topof my head, logically I would change the lines from 71 to 78 as follows

 for i=1,#list do  
 print("list[" .. i .. "]= " .. list[i])  
 local tempFile = remoteDirectory .. list[i]  
  
 network.download(tempFile, "GET", LoadRemoteImage, list[i], system.TemporaryDirectory )  
 end  

Just worried that you are overwriting your files to “helloCopy.png”, use the same name as they have and use them. If you want to then delete, you can too.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15724 reply_id: 58118[/import]

@Carlos: Whoa! Your picture is larger than life! :slight_smile:

The problem isn’t getting my filenames, I get the filenames all passed back to me just fine thru my php script. The problem is EXACTLY as Jayant points out, I don’t know how to increment the name of the file being COPIED to my temp directory, i.e. it’s ALWAYS named “helloCopy.png”… :confused:

from Jayant: “use the same name as they have and use them. If you want to then delete, you can too.”

The problem with that is that other people will be uploading their own images, with unique names. I don’t have the luxury of naming them all sequential like “image1.png,image2.png…etc.”

So, I kludged together this workaround. KLUDGE is the key word here, but hell, if it works, I’m not complaining. Just making sure I wasn’t missing something simple.

Kludged code:

--  
-- Project: LevelDownloadTest  
-- Description:   
--  
-- Version: 1.0  
-- Managed with http://CoronaProjectManager.com  
--  
-- Copyright 2011 Mario Roberti. All Rights Reserved.  
--   
remoteDirectory = "http://www.iswdev.com/stuff/battlemaps/mapthumbs/"  
gameBoard = display.newGroup()  
fileCount = 0  
function split(pString, pPattern)  
 local Table = {} -- NOTE: use {n = 0} in Lua-5.0  
 local fpat = "(.-)" .. pPattern  
 local last\_end = 1  
 local s, e, cap = pString:find(fpat, 1)  
 while s do  
 if s ~= 1 or cap ~= "" then  
 table.insert(Table,cap)  
 end  
 last\_end = e+1  
 s, e, cap = pString:find(fpat, last\_end)  
 end  
 if last\_end \<= #pString then  
 cap = pString:sub(last\_end)  
 table.insert(Table, cap)  
 end  
 return Table  
end  
local function LoadRemoteImage( event )  
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 fileCount = fileCount + 1  
 print("Filecount "..fileCount)  
 end  
  
 print ( "RESPONSE: " .. event.response )  
end  
  
function LoadImagesFromTempDirectory()  
 for i=1,fileCount-1 do  
 local tempImage = display.newImage( "thumb"..i..".jpg", system.TemporaryDirectory)  
 tempImage.alpha = 0  
 gameBoard:insert(tempImage)  
 transition.to( tempImage, { alpha = 1.0 } )  
 gameBoard:insert(tempImage,false)  
 tempImage:toBack()  
 tempImage.x = 100  
 tempImage.y = i\*110  
 local bounds = tempImage.contentBounds  
  
 local tempText = display.newText(gameBoard,"File: "..list[i].."\nDescription: Heroic adventurers evade death and dismemberment!", bounds.xMax, tempImage.y, nil, 15)  
 end  
end  
  
function LoadRemoteText( event )  
 -- OK so my PHP script returnsa CSV string.  
 -- We'll parse that into a table using the   
 -- above 'split' function. No problem!  
  
  
 -- list=split(string\_to\_split,pattern\_to\_match)  
 -- e.g.:  
  
 list=split(event.response,",")  
 -- OK the table 'list' contains  
 -- my file names.  
  
 -- Gives us:  
 -- traderstop3thumb.jpg  
 -- withoutpitthumb.jpg  
 -- mineentrancethumb.jpg  
 -- dreestonsurroundsthumb.jpg  
 -- stormwatchinnthumb.jpg  
  
 for i=1,#list do  
 print("list["..i.."] "..list[i])  
 local tempFile = remoteDirectory..list[i] -- Concatenate my directory and my filename  
 -- Do the call to load the  
 network.download(tempFile, "GET", LoadRemoteImage, "thumb"..i..".jpg", system.TemporaryDirectory )  
 end  
  
 -- LoadImagesFromTempDirectory()  
  
end  
  
----------------------------------------------------------------  
-- MAIN LOOP  
----------------------------------------------------------------  
local function main( event )  
 if(fileCount==5)then  
 fileCount=6  
 print("Load them all!")  
 LoadImagesFromTempDirectory()  
 end  
end  
  
Runtime:addEventListener( "enterFrame", main )  
  
   
network.request( remoteDirectory.."enumerateImages.php", "GET", LoadRemoteText )  
  

[import]uid: 11636 topic_id: 15724 reply_id: 58122[/import]

Hey there,

Did you ever get a solution to your problem?

I am trying to work out a way to process a whole load of downloads sent from my server. I’m struggling with the fact that there is no download complete event for network.download so you have to wait for the actual file to download using a timer to check its status.

I am probably making things too complicated, but I’m using globals as status flags whereas I would like to raise events and respond to them when downloads complete.

did you have any joy?

JB [import]uid: 58328 topic_id: 15724 reply_id: 117347[/import]