Corona SDK Quick Tip: Create a Screen Capture Utility

Corona SDK Quick Tip: Create a Screen Capture Utility

Tutorial Details
  • Technology: Corona SDK
  • Difficulty: Beginner
  • Estimated Completion Time: 30 Minutes

In this tutorial, you’ll learn how to take a screenshot of your application and review it in the device photo library.


Application Overview

Corona SDK - Figure 1

Using the Corona display class and the captureScreen() method, we’ll capture the current screen, save it, and then review it in the photo library.


Select Target Device

Corona SDK - Figure 2

The first thing you have to do is select the platform you want to run your app, this way you’ll be able to choose the size for the images you will use.

The iOS platform has these characteristics:

  • iPad: 1024x768px, 132 ppi
  • iPhone/iPod Touch: 320x480px, 163 ppi
  • iPhone 4: 960x640px, 326 ppi

Because Android is an open platform, there are many different devices and resolutions. A few of the more common screen characteristics are:

  • Nexus One: 480x800px, 254 ppi
  • Droid: 854x480px, 265 ppi
  • HTC Legend: 320x480px, 180 ppi

In this tutorial we’ll be focusing on the iOS platform, specifically developing for distribution to an iPhone/iPod touch.


Interface

Corona SDK - Figure 3

A simple interface featuring a single button will be used. This button will be linked to a function that will be performed when tapped.


Exporting PNG’s

Corona SDK - Figure 4

Depending on the device you have selected you will need to export the graphics in the recommended PPI, you can do that in your favorite image editor.

I used the Adjust Size… function in the Preview app in Mac OS X.

Remember to give the images a descriptive name and to save them in your project folder.


Code!

Corona SDK - Figure 5

Time to write our application!

Open your prefered Lua editor (any Text Editor will work, but you won’t have syntax highlighting) and prepare to write your awesome app.


Hide Status Bar

First, we hide the status bar, this is the bar on top of the screen that shows the time, signal and other indicators.

display.setStatusBar(display.HiddenStatusBar)

Add Background

Now we add the background:

local background = display.newImage('background.png')

This line creates the local variable background and uses the display API to add the specified image to the stage. By default, the image is added to 0,0 using the top left corner as the reference point.


Place Screenshot Button

The following lines add the Capture button to the screen and place it in the center.

local captureButton = display.newImage('captureButton.png')
captureButton:setReferencePoint(display.CenterReferencePoint)
captureButton.x = display.contentWidth * 0.5
captureButton.y = display.contentHeight * 0.5

Capture Screen Function

This function will run when the capture button is activated, it responds to a tap event.

function captureButton:tap(e)
end

We’ll add the listener later in the code.


Capture Screen

The screenshot code.

It declares a variable named screenshot that will hold the image information in case you want to make aditional manipulations to it.

local screenshot = display.captureScreen(true)

A parameter is used in the captureScreen method, a boolean to indicate if the image will be saved to the device photo library.


Open Photo Library

With your screenshot already stored, it’s time to open the photo library to check what it looks like.

media.show(media.PhotoLibrary)

This will open the Photo Library to let you browse through your pictures and find your screenshot.


Listener

The next line adds the required listener to the capture button.

captureButton:addEventListener('tap', captureButton)

Icon

Corona SDK - Figure 6

If everything is working as expected, we are almost ready to build our app for device testing. Just one more thing, our application icon.

Using the graphics you created before you can create a nice and good looking icon, the icon size for the iPhone icons is 57x57px, but the iTunes store uses a 512x512px so it’s better to create your icon in this size.

It doesn’t need to have the rounded corners or the transparent glare, iTunes and the iPhone will do that for you.


Conclusion

Use this example application and try adding features of your own.

Thanks for reading this tutorial, I hope you’ve found it useful!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Matt Privman

    Thanks a lot for making this tutorial. Corona SDK looks like a much better option to learning Objective-C.

  • KWags

    As far as I can tell, this does not work correctly for landscape orientation — works in their simulator, but not on an actual iPhone.

    Have you found different?

  • http://www.francomusso.com Franco Musso

    I found this extremely easy to follow and very well explained.
    Can’t believe how quickly I got results compared to the heaps of Xcode tutorials I’ve tried to follow!
    Thanks so much :)

  • Margalus

    Nice tutorial, with few lines of code, but it didn’t work for me. I tested it on a Samsung Galaxy S(creating a rectangle and centering it instead of using the image) but it prompts me the Library immediately without tapping the button, in fact it opens it so fast i don’t even see the background and my button.

    • Margalus

      I’ve managed to fix it. You should write which lines are under the function, i was distracted and i compiled it adding line per line and i had the call to the Photo Library outside the function..it works now but try to be more specific in the future, still a great job :)

  • ron

    Hi,

    Thanks so much for the tutorial. This is really helpful. I just started learning the corona sdk and mobile app development… no coding knowledge at all.

    I tried the code about but didn’t work as expected for me. Could someone help me? I pasted my code below… every time I launch this is will take screenshot automatically and save the pic on my desktop… help please …..

    display.setStatusBar(display.HiddenStatusBar);

    local background = display.newImage(‘background.png’);
    background.xScale = 2
    background.yScale = 2
    background.x = 320
    background.y = 480

    local captureButton = display.newImage(‘button.png’);

    captureButton:setReferencePoint(display.CenterReferencePoint);
    captureButton.x = 320
    captureButton.y = 480

    function captureButton:tap(e)

    end;

    local screenshot = display.captureScreen(true);

    media.show(media.PhotoLibrary);

    captureButton:addEventListener(‘tap’, captureButton);

    Thank you in advance.