Ios 使用AirPrint打印UIView的内容会导致空页
我想用AirPrint打印机打印UIView的内容。UIView通过drawrect:消息以编程方式绘制,并显示在iPhone或iPad显示屏上。 打印的图像应适合默认页面,以便与iPhone或iPad上的图像不同。因此,我使用drawrect:forViewPrintFormatter:方法将UIView的内容输出到AirPrint打印机。 为此,我将UIPrintInteractionController与UIViewPrintFormatter和UIPrintPageRenderer结合使用 我尝试了以下选项,结果如下:Ios 使用AirPrint打印UIView的内容会导致空页,ios,objective-c,uiview,printing,drawrect,Ios,Objective C,Uiview,Printing,Drawrect,我想用AirPrint打印机打印UIView的内容。UIView通过drawrect:消息以编程方式绘制,并显示在iPhone或iPad显示屏上。 打印的图像应适合默认页面,以便与iPhone或iPad上的图像不同。因此,我使用drawrect:forViewPrintFormatter:方法将UIView的内容输出到AirPrint打印机。 为此,我将UIPrintInteractionController与UIViewPrintFormatter和UIPrintPageRenderer结合使
- 如果我将numberOfPages设置为1,则为打印的空白页李>
- 如果我将numberOfPages设置为2,则所需图像将打印两次
@implementation AHLPrintPageRenderer
- (NSInteger)numberOfPages
{
return 2
}
- (void) drawPrintFormatter:(UIPrintFormatter *)printFormatter forPageAtIndex:(NSInteger)pageIndex
{
[printFormatter drawInRect:self.printableRect forPageAtIndex:0];
}
- (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
}
}
打印UIView自定义子类的printView实例的代码:
@implementation AHLPrintPageRenderer
- (NSInteger)numberOfPages
{
return 2
}
- (void) drawPrintFormatter:(UIPrintFormatter *)printFormatter forPageAtIndex:(NSInteger)pageIndex
{
[printFormatter drawInRect:self.printableRect forPageAtIndex:0];
}
- (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
}
}