Tutorial: Image Printing via Corona Enterprise (iOS)

There have been plenty of requests for the ability to print from Corona SDK, but given that Corona SDK is all done with graphics in OpenGL, exactly what would you want to print? Well, one example would be to print a screenshot of your app. Another usage case would be printing downloaded PDFs.

Corona SDK doesn’t have built-in printing facilities, so how can you accomplish this? With Corona Enterprise of course! As a bonus, we’ll also demonstrate how to save an image from the system.DocumentsDirectory to the camera roll.

NOTE: This tutorial is for iOS and assumes that you have a wireless printer on the same WiFi network that supports the Apple AirPrint protocol.

Getting up to Speed

Before you proceed, you should read and understand the Corona Enterprise Quickstart (iOS) tutorial because many aspects of this tutorial build upon the lessons learned therein. Please follow all of the steps from that tutorial to set up a new project and name it whatever you prefer. This tutorial will build a plugin called image which you can use within your Corona SDK project.

Xcode Project Modifications

To begin, let’s go through the Xcode modifications which will allow this plugin to function. First, edit the AppCoronaDelegate.h file and change this line…

This will allow your app to talk to the iOS Print Interaction Controller.

Now, let’s modify the PluginLibrary.mm file. In the PluginLibrary class where you had set up definitions for the on and off functions in the previous tutorial, rename one definition to saveImage and the other to printImage. Essentially, your class should look like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

classPluginLibrary

public:

typedefPluginLibrary Self;

public:

staticconstcharkName[];

staticconstcharkEvent[];

protected:

PluginLibrary();

public:

boolInitialize(CoronaLuaRef listener);

public:

CoronaLuaRef GetListener()constreturnfListener;

public:

staticintOpen(lua_State *L);

protected:

staticintFinalizer(lua_State *L);

public:

staticSelf*ToLibrary(lua_State *L);

public:

staticintinit(lua_State *L);

staticintsaveImage(lua_State *L);

staticintprintImage(lua_State *L);

private:

CoronaLuaRef fListener;

};

Next, a few lines down from this, change the PluginLibrary::kName value to "plugin.image". This will let you reference the plugin with that name in your Lua code. Also change the PluginLibrary::kEvent[] value to "image":

1

2

3

4

5

6

7

8

9

10

11

// This corresponds to the name of the library, e.g. [Lua] require "plugin.library"

constcharPluginLibrary::kName[]="plugin.image";

// This corresponds to the event name, e.g. [Lua] event.name

constcharPluginLibrary::kEvent[]="image";

PluginLibrary::PluginLibrary()

:fListener(NULL)

Several lines below this, in the PluginLibrary::Open method, you’ll need to update the kVTable array to represent the mapping of the Lua function to the Objective-C method:

1

2

3

4

5

6

7

8

9

// Functions in library

constluaL_Reg kVTable[]=

"init",init,

"saveToAlbum",saveImage,

"airPrint",printImage,

NULL,NULL

};

Now, let’s write the two functions to handle printing and saving of the image. Because of the setting in the table above, "airPrint" will be the function name on the Lua side, while printImage defines the Xcode method. Since this tutorial is primarily about printing, let’s look at the printImage function first:

NSLog(@"FAILED! due to error in domain %@ with error code %u",error.domain,error.code);

};

[pic presentAnimated:YES completionHandler:completionHandler];

}

return0;

}

This creates a new NSString object called path that is initialized with the first item passed to the Lua parameter stack. It’s expected to be a string and an error will be thrown if not. On the next line, we create an NSData object called myData initialized with the contents of the file path. The remainder of the code sets up the print interaction controller. If your app can print, we set up various parameters needed for that process. If you wish to explore the source and logic behind this code, please see here.

Now, let’s write the saveImage function. This will be named "saveToAlbum" on the Lua side, while saveImage will be the Xcode method.

1

2

3

4

5

6

7

8

9

10

11

// [Lua] library.saveImage( word )

intPluginLibrary::saveImage(lua_State *L)

NSString *path=[NSString stringWithUTF8String:lua_tostring(L,-1)];

UIImage *image=[UIImage imageWithContentsOfFile:path];

UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);

return0;

Like the printImage function, this gets the image path passed in as the first parameter. It then generates a UIImage object based on the contents of the file at path and writes it to the camera roll.

Lua Side

Now we’ll tackle the Lua/Corona side! In your main.lua file, paste this code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

local image=require("plugin.image")

local printMeButton

local functionhandleButtonEvent(event)

local imageName=system.pathForFile("myimage.jpg",system.ResourceDirectory)

image.airPrint(imageName)

returntrue

end

printMeButton=widget.newButton

id="button1",

label="Print Photo",

labelColor=default=0,0,0,over=0,0,0},

onRelease=handleButtonEvent

}

printMeButton.x=display.contentCenterX

printMeButton.y=display.contentCenterY-50

image.init(listener)

Effectively, this code includes your custom plugin and creates a widget button with the label Print Photo. When the button is tapped, it calls the handleButtonEvent() function which does the magic. First, it generates a path to the image you want to print (this could also be a PDF). In this case, it picks up a file named myimage.jpg from the app’s bundle (system.ResourceDirectory). Alternatively, you could use the display.save() API to capture the screen to the system.DocumentsDirectory and pick the image from there.

With the path to that image, we call the plugin function called airPrint() (the printImage method in Objective-C).

If you wish to test the saving function, the Lua code should be almost identical, except that you’d call image.saveToAlbum( path ) instead of image.airPrint( path ).

If everything went well, you should now be able to build for a device and install it as described in the quickstart tutorial.

Conclusion

While Corona Enterprise may seem intimidating at first, utilizing it can empower your apps with great new features. Hopefully this tutorial has shown you the basic framework and will encourage you to explore new potential ideas in development.

Disclaimer: SDKNews.com only syndicates the blog entries from various SDK blogs.
We are not the creator/author of these entries (posts). Product names, brand names
and company names mentioned on this site may be trademarks of their respective owners.