return loadRemoteImage from url or load locally if exists

Hej,

did anybody succeeded in writing a function, that loads a remote image from an url or locally if already downloaded and returns that display object?

I don’t really like, that loadRemoteImage is not returning a displayObject, but just creating one, so doing lists is getting really annoying.

Any ideas?

Thanks,

Phil

This ONLY works for pro or higher.

local easyRemoteImage = function( curImg, fileName, imageURL ) if( string.match( imageURL, "http" ) == nil ) then imageURL = "http:" .. imageURL end if( io.exists( fileName, system.TemporaryDirectory ) ) then curImg.fill = { type = "image", baseDir = system.TemporaryDirectory, filename = fileName } return end local function networkListener( event ) if ( event.isError ) then --print( "Network error - download failed" ) elseif ( event.phase == "began" ) then --print( "Progress Phase: began" ) elseif ( event.phase == "ended" ) then --print( "Displaying response image file" ) curImg.fill = { type = "image", baseDir = event.response.baseDirectory, filename = event.response.filename } end end local params = {} params.progress = false network.download( imageURL, "GET", networkListener, params, fileName, system.TemporaryDirectory ) end

Hi.  I think the usage should be clear, but just in case:

-- pseudo code local tmp = newRect( ... ) local imgUrl = "..." local fileName = "something.png" easyRemote( tmp, fileName, imgURL ) 

If ‘something.png’ is found localy in the temporary folder it will be used, if not we’ll request it from the URL and if it gets returned, save it in ‘something.png’ in the temporary folder.  We will also ‘late fill’ the rect with the returned image.

Hej, thanks for replying.

I tried your function but when I try:

                   local curImg                     easyRemoteImage( curImg, fileName, imageURL )                     print(curImg.fill.filename)

it returns nil.

So how does this return the actual fileName, so I can use it in a loop to create a gallery?

Cheers,

Phil

OH GREAT!

The idea of later fill a rect wasn’t obvious to me. thanks a lot, that solves my issue :slight_smile:

I just noticed that I used io.exists() from my own library in that example.

Here is the code for the exists() extension to io.*

function io.exists( fileName, base ) local base = base or system.DocumentsDirectory local fileName = fileName if( base ) then fileName = system.pathForFile( fileName, base ) end if not fileName then return false end local f=io.open(fileName,"r") if (f == nil) then return false end io.close(f) return true end

Cheers,

had an ifExists function already, but always good to share code ^^

Thanks

 Great. 

 I haven’t noticed that we can use object.fill like that

 Thanks

This ONLY works for pro or higher.

local easyRemoteImage = function( curImg, fileName, imageURL ) if( string.match( imageURL, "http" ) == nil ) then imageURL = "http:" .. imageURL end if( io.exists( fileName, system.TemporaryDirectory ) ) then curImg.fill = { type = "image", baseDir = system.TemporaryDirectory, filename = fileName } return end local function networkListener( event ) if ( event.isError ) then --print( "Network error - download failed" ) elseif ( event.phase == "began" ) then --print( "Progress Phase: began" ) elseif ( event.phase == "ended" ) then --print( "Displaying response image file" ) curImg.fill = { type = "image", baseDir = event.response.baseDirectory, filename = event.response.filename } end end local params = {} params.progress = false network.download( imageURL, "GET", networkListener, params, fileName, system.TemporaryDirectory ) end

Hi.  I think the usage should be clear, but just in case:

-- pseudo code local tmp = newRect( ... ) local imgUrl = "..." local fileName = "something.png" easyRemote( tmp, fileName, imgURL ) 

If ‘something.png’ is found localy in the temporary folder it will be used, if not we’ll request it from the URL and if it gets returned, save it in ‘something.png’ in the temporary folder.  We will also ‘late fill’ the rect with the returned image.

Hej, thanks for replying.

I tried your function but when I try:

                   local curImg                     easyRemoteImage( curImg, fileName, imageURL )                     print(curImg.fill.filename)

it returns nil.

So how does this return the actual fileName, so I can use it in a loop to create a gallery?

Cheers,

Phil

OH GREAT!

The idea of later fill a rect wasn’t obvious to me. thanks a lot, that solves my issue :slight_smile:

I just noticed that I used io.exists() from my own library in that example.

Here is the code for the exists() extension to io.*

function io.exists( fileName, base ) local base = base or system.DocumentsDirectory local fileName = fileName if( base ) then fileName = system.pathForFile( fileName, base ) end if not fileName then return false end local f=io.open(fileName,"r") if (f == nil) then return false end io.close(f) return true end

Cheers,

had an ifExists function already, but always good to share code ^^

Thanks

 Great. 

 I haven’t noticed that we can use object.fill like that

 Thanks