Corona SDK Pro Tip of the Day #11
Easily take screenshots of your app or game with a press of a single button!
Corona now supports input from a keyboard. Let's bind "S" key to capture screenshots with
Put this code in your
Runtime:addEventListener('key', function (event) if event.keyName == 's' and event.phase == 'down' then local scene = storyboard.getScene(storyboard.getCurrentSceneName()) if scene and scene.view then display.save(scene.view, display.pixelWidth .. 'x' .. display.pixelHeight .. '_' .. math.floor(system.getTimer()) .. '.png') return true end end end)
This adds a listener for 'key' events. Inside the listener we check if the pressed key is 's'.
'down' phase represents the moment the key was pressed, not released.
Then we need to get the current scene being displayed and save it's content with
display.save(). I use here
storyboard, but it's easy to convert to
local scene = composer.getScene(composer.getSceneName('current'))
display.save() we give the screenshot a distinct filename with a format
WIDTHxHEIGHT_time. Screenshots are saved to Documents directory inside project's sandbox.
And because some devices can have keyboards attached, use it only in the Simulator.
It is also available as a GitHub Gist.