Printing images (iOS 4)

AirPrint comes with iOS 4.2 and is available to both the iPhone and iPad. The AirPrint user interface on the iPhone and iPad is shown in figure 11.4. Generally, the print button is bar button item. When the user taps the Print button, the view controller to assign the printing task will present as a modal view controller on the iPhone and a popover view on the iPad. Once the print task is assigned, it will be printed right away or it will wait in the print queue. Users can check on the status by accessing the Print Center under the multitasking UI.

AirPrint is handled by the iOS system’s UIKit, and no extra framework is required for the project.

The UIWebView, UITextView, and data such as UIImage and PDF files are print ready and can be handled by the print controller directly.

In this section you’ll learn how to print an image from the application with the UIPrintInteractionController on the iPhone and iPad. Before we start coding, let’s examine the printing workflow.

Printing workflow

Inside the AirPrint API, you can create the UIPrintInteractionController and present it as a modal view controller in the iPhone or a popover view on the iPad. It works the same as the system printing UI, as shown in figure 11.4.

UIPrintInteractionController is the key class in iOS for printing. You can create a printing user interface by calling the following code:


tmp5A67_thumb

 

 

 

Printing UI on the iPad and iPhone

Figure 11.4 Printing UI on the iPad and iPhone

To make sure the print view controller is available in the current system, you can use the method [UIPrintInteractionController isPrintingAvailable] to check the availability.

Next, you need to define or customize the print task by setting the properties of the controller. There are some important properties for the controller listed in table 11.4.

In order to define the printInfo, you need to create an instance of UIPrintInfo. UIPrintInfo is a class that allows you to customize the printing job’s information. UIPrintInfo includes properties such as the print-job name, the printer identifier, the orientation of the printed content, the duplex mode, and the kind of content (general, photo, or grayscale).

Similar to the UIImagePickerController, make sure you are implementing the UIPrintInteractionController’s delegate method to handle the callback messages. For example, when the print task is assigned, show an alert view to notify the end user.

Table 11.4 A few properties in UIPrintInteractionController

Property

Summary

printingltem

A single Ullmage, NSData, NSURL, or ALAsset object containing or referencing image data or PDF data.

printlnfo

A UIPrintInfo object to customize the printItem.

printingltems

An array of objects either containing or referencing image data or PDF data. These objects are directly printable.

printFormatter

A UIPrintFormatter object handles the printing format.

printPageRender

An instance of a custom class of UIPrintPageRenderer draws each page of printable content partially or entirely.

In order to present the print view controller on the iPhone, you must create the completion handler and present it with method present-Animated:completionHandler:; on the iPad, present the popover controller with the method presentFromBarButtonItem:animated:completion Handler: .

Simulating printing

Luckily, the iOS SDK after 4.2 comes with the Air-Print simulator app for Mac OS in case you don’t have a printer for testing. You can find this print simulator app at <Xcode>/Platforms/iPhoneOS .platform/Developer / Applications/Printer Simulator, as shown in figure 11.5.

Printer Simulator under the iOS SDK

Figure 0.1 Printer Simulator under the iOS SDK

Printer Simulator screenshot

Figure 11.6 Printer Simulator screenshot

Launch the Printer Simulator app on your Mac. You will see a message similar to the one shown in figure 11.6.

With the Printer Simulator running, you can test the printing tasks directly from the iOS Simulator. Now we will start coding for printing.

Creating a demo app-printing image

In this section, we will create a simple view-based application for the iPhone and iPad containing an image in the center, which will print when the user taps the Print button.

Fire up Xcode and create a new project with View-Based Application template under iOS. Name it iPrint. Drag a photo you would like to print to this project’s Resources folder.

Select the iPrintViewController header file and add in the changes shown in the following listing.

Listing 11.5 iPrintViewController header file

Listing 11.5 iPrintViewController header file

With the image view, Print button, and printPhoto: method added, let’s drag and hook up the two subviews to iPrintViewController’s nib file visually. Connect the method printPhoto: to the Print button’s action.

Now add in the following code to the view controller’s implementation file to complete the print task.

Listing 11.6 iPrintViewController implementation file

Listing 11.6 iPrintViewController implementation file

 

 

Listing 11.6 iPrintViewController implementation file

 

 

 

Listing 11.6 iPrintViewController implementation file

In the viewDidLoad: method, you first check the availability of the print view controller o. If it’s not currently available on iOS, an alert view will pop up to notify the user. The print job is defined in the printPhoto: method ©. First, create the print view controller, and then define the delegate and the print info. In this example, the printing item is the image from the image view. Then, define the block for the completion handler. You want to monitor the error message in this example:

tmp5A75_thumb

On the iPad, the print view controller will show as a popover controller from the bar button; on the iPhone, the print view controller will present as a modal view controller.

When the printing job is finished, the delegate: method © will be called, so you notify the user with an alert view. That’s all!

Launching the printer app on the Simulator

Now save all the changes. Before you build and run this iPrint app, make sure the printing simulator app is running. When the app is launched in the Simulator, tap the Print button. You will see that the simulator printer is available on the print view controller, as shown in figure 11.7.

You can play around with this app. For example, you can set the print info’s property to change the content to grayscale. Even better, you can change the input image to one of the photos from the photo library.

That’s all! Now you’ve learned how to print out image with AirPrint and test it in iOS.

iPrint app running on the Simulator for the iPhone and iPad

Figure 11.7 iPrint app running on the Simulator for the iPhone and iPad

Summary

Dealing with media is a huge topic that probably could fill a topic on its own. Fortunately, there are relatively easy (if limited) ways to utilize each major sort of media. In this topic, we discussed the various ways to manage and manipulate images on the iPhone and iPad. We first discussed how to load them from disk. This includes images saved in an application’s directory as well as from the camera roll.

We also showed you how the UIImagePickerController can be slightly modified to allow the user to take a photo and use it in an application.

You’ve seen how all these pictorial fundamentals work together, so we’re now ready to move on to the next major types of media: audio and video.

Next post:

Previous post: