Hi guys I’ve been away for a while but come back. To the point, I want to capture, load and reset the display object resulting from media capture and select image.
my code
local image local photo local camButton local selectPhoto local refreshButton -- create() function scene:create( event ) local sceneGroup = self.view --cam local sessionComplete = function(event) image = event.target if ( image ) then print( "media.capturePhoto() returned an image!" ) print( "Image resolution: ".. image.width .. "x" .. image.height ) -- Center image image.x = centerX image.y = centerY sceneGroup:insert( image ) image.width = 300 image.height = 300 else print( "media.capturePhoto() was canceled." ) end end local function capturePhoto( event ) display.remove( image ) --delete image to capture another if photo then --if there are a photo selected remove it photo:removeSelf() photo = nil end if media.hasSource( media.Camera ) then media.capturePhoto( { listener=sessionComplete } ) else native.showAlert( "App", "This device does not have a camera.", { "OK" } ) end end --select photo local sessionComplete = function(event) photo = event.target if photo then print( "media.selectPhoto() user select a photo!" ) -- Center photo photo.x = centerX photo.y = centerY sceneGroup:insert( photo ) photo.width = 300 photo.height = 300 else print( "No photo Selected" ) end end local function pickPhoto( event ) display.remove( photo ) --delete photo to select another if image then --if there are a image captured remove it image:removeSelf() image = nil end media.selectPhoto( { mediaSource = media.SavedPhotosAlbum, listener = sessionComplete, origin = selectPhoto.contentBounds, permittedArrowDirections = { "right" }, }) end --refresh local function refreshButton( event ) if ( event.phase == "began" ) then --audio.play( some audio ) elseif ( event.phase == "ended" ) then if image then --here is the problem display.remove( image ) end if photo then display.remove( photo ) end end return true end --buttons --cam camButton = widget.newButton( { width = 66, height = 66, defaultFile = "images/buttons/cam.png", overFile = "images/buttons/cam.png", } ) -- Center cam btn camButton.x = display.contentWidth-610 camButton.y = display.contentHeight-397 sceneGroup:insert( camButton ) camButton:addEventListener( "tap", capturePhoto ) --photo selectPhoto = widget.newButton( { width = 66, height = 66, defaultFile = "images/buttons/select.png", overFile = "images/buttons/select.png", } ) -- Center photo btn selectPhoto.x = display.contentWidth-535 selectPhoto.y = display.contentHeight-397 sceneGroup:insert( selectPhoto ) selectPhoto:addEventListener( "tap", pickPhoto ) -- refresh btn refreshButton = widget.newButton( { width = 35, height = 35, defaultFile = "images/buttons/refresh.png", overFile = "images/buttons/refresh.png", onEvent = refreshButton } ) --center refresh button refreshButton.x = centerX+200 --150 refreshButton.y = centerY+120 sceneGroup:insert( refreshButton ) end
I think the problem is scope or a bad code for remove image from cam or select,
the code contains typos. I get an error something about trying to remove a nil object.
Any help guys?