I want to print the content of an UIView with an AirPrint printer. The UIView is drawed programatically with the drawrect: message and shown on the iPhone or iPad display. The printed image should fit into a default page so it will be different from the image on the iPhone or iPad. So I am using the drawrect:forViewPrintFormatter: method to output the content of the UIView to an AirPrint printer. For this purpose, I am using the UIPrintInteractionController in conjunction with an UIViewPrintFormatter and an UIPrintPageRenderer.
I have tried the following options with following results:
- If I am using a customized subclass of UIPrintPageRenderer, I obtain:
- an empty page printed, if I set numberOfPages = 1;
- the desired image printed twice, if I set numberOfPages = 2
- If I am using the UIPrintFormatter without the printPageRenderer, I am obtaining the desired image view printed followed by an empty page.
I would be extremely grateful if someone could help me
Here the code in the UIPrintPageRenderer subclass:
@implementation AHLPrintPageRenderer
- (NSInteger)numberOfPages
{
return 2
}
- (void) drawPrintFormatter:(UIPrintFormatter *)printFormatter forPageAtIndex:(NSInteger)pageIndex
{
[printFormatter drawInRect:self.printableRect forPageAtIndex:0];
}
Code to print the printView instance of an UIView custom subclass:
- (void) printDiagram
{
// Obtain the shared UIPrintInteractionController
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
if(!controller){
NSLog(@"Couldn't get shared UIPrintInteractionController!");
return;
}
else
[controller setDelegate:self];
// We need a completion handler block for printing.
UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(completed && error)
NSLog(@"FAILED! due to error in domain %@ with error code %ld", error.domain, (long)error.code);
};
// Obtain a printInfo so that we can set our printing defaults.
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
// This application produces General content that contains color.
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"printJob";
printInfo.duplex = UIPrintInfoDuplexLongEdge;
CGSize viewSize = printView.bounds.size;
if (viewSize.height > viewSize.width) {
printInfo.orientation = UIPrintInfoOrientationPortrait;
}
else {
printInfo.orientation = UIPrintInfoOrientationLandscape;
}
controller.printInfo = printInfo;
controller.showsPageRange = YES;
UIViewPrintFormatter *formatter = [printView viewPrintFormatter];
//
// The following three lines of code should be commented out if printing with the UIPrintFormatter only
//
AHLPrintPageRenderer *renderer = [[AHLPrintPageRenderer alloc] init];
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
controller.printPageRenderer = renderer;
// ****
controller.printFormatter = formatter;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[controller presentFromBarButtonItem:printButton animated:YES completionHandler:completionHandler]; // iPad
}
else {
[controller presentAnimated:YES completionHandler:completionHandler]; // iPhone
}
}