Load image using basedir option in newImageRect

Im new to solar2d and studying the media api

I tried saving an image using the media.selectPhoto and save it to a file.
media.selectPhoto( { mediaSource=media.PhotoLibrary, listener=onComplete,destination = { baseDir=system.TemporaryDirectory, filename=“image.jpg” } } )

Tried loading it with newImageRect using this syntax
imgMenu = display.newImageRect(“image.jpg”,{baseDir=system.TemporaryDirectory},50,50)

its not loading. Any idea?

Try this,

imgMenu = display.newImageRect( "image.jpg", system.TemporaryDirectory, 50, 50 )

if “system.TemporaryDirectory” is not present, it will assume default of “system.ResourceDirectory”. Please see the doc here for more details.
https://docs.coronalabs.com/api/library/display/newImageRect.html

I tried but the imgMenu is not updating the image it should show. I changed it to default which is ResourceDirectory but still nothing.

Did you follow the example here?
https://docs.coronalabs.com/api/library/media/selectPhoto.html

Follow the example on the doc in terms of event / listener flow, except, you can modify the “onComplete()” function as below,

local function onComplete( event )
    if event.completed then 
        imgMenu = display.newImageRect( "image.jpg", system.TemporaryDirectory, 50, 50 )
    end
end

Yes. I forgot to mention, the imgMenu has an existing image.

Im using the selectPhoto function to select a new photo to display in imgMenu variable.

But i have no luck at the moment.

Perhaps, you could share the relevant pieces of code here, so that the community can help to take a look.

This is a piece of my code:

local function onComplete( event )
local photo = event.target

if photo then
  print("Photo available")
else
  --This is where I will swap the image of the imgMenu variable
  --I tried removing the variable but it returns an "nil value" error
  imgMenu:removeSelf()
  imgMenu = display.newImageRect("image.jpg",system.ResourceDirectory,50,50)
  imgMenu.anchorX=0
  imgMenu.x =20
  imgMenu.y=100

 end

end

local function pickPhoto( event )

if media.hasSource( media.PhotoLibrary ) then
media.selectPhoto( { mediaSource=media.PhotoLibrary, listener=onComplete,destination = { baseDir=system.ResourceDirectory,filename=“image.jpg” } } )
else
txtnotice.text= “This device does not have a photo library.”
end
end

function scene:create( event )
local sceneGroup = self.view
imgMenu = display.newImageRect(“images/buttons/photo.jpg”,50,50)
imgMenu.anchorX=0
imgMenu.x =20
imgMenu.y = txttitleTab2.y + 50

  imgMenu:addEventListener( "tap", pickPhoto )
  sceneGroup:insert(imgMenu)

end

How about removing the section from “if photo then” to “end”, and replacing the section with the following,

if event.completed then 
    display.remove( imgMenu )
    imgMenu = display.newImageRect( "image.jpg", system.TemporaryDirectory, 50, 50 )
    imgMenu.anchorX=0
    imgMenu.x =20
    imgMenu.y=100
end

Please give it a try, and let us (the community) know about the results.

It worked! Thanks a lot!

it seems i have to dump it in the temporaryDirectory. Coz i tried using the ResourceDirectory, it gives me “nil value” error

I am glad it works. You are right about the directory. There is another possible cause on your earlier codes, which is,

  • Your “imgMenu” recreation from loaded photo is outside the “if photo then”, and in the “else” block.
  • The “photo” as you earlier defined is, “event.target”, which is the return display object of the loaded photo.
  • So, when there is a loaded photo, your earlier code say, just do a "print( ‘photo available’ ).

In any case, I would prefer to check for an event status, in this case, “event.completed”, to ensure the proper flow.

All the best to your coding and learning journey!

If you are trying to save to “ResourceDirectory” you might want to check its limitations:

https://docs.coronalabs.com/api/library/system/ResourceDirectory.html