Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用AirPrint打印UIView的内容会导致空页_Ios_Objective C_Uiview_Printing_Drawrect - Fatal编程技术网

Ios 使用AirPrint打印UIView的内容会导致空页

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结合使

我想用AirPrint打印机打印UIView的内容。UIView通过drawrect:消息以编程方式绘制,并显示在iPhone或iPad显示屏上。 打印的图像应适合默认页面,以便与iPhone或iPad上的图像不同。因此,我使用drawrect:forViewPrintFormatter:方法将UIView的内容输出到AirPrint打印机。 为此,我将UIPrintInteractionController与UIViewPrintFormatter和UIPrintPageRenderer结合使用

我尝试了以下选项,结果如下:

  • 如果我使用UIPrintPageRenderer的自定义子类,我将获得:
    • 如果我将numberOfPages设置为1,则为打印的空白页
    • 如果我将numberOfPages设置为2,则所需图像将打印两次
  • 如果在没有printPageRenderer的情况下使用UIPrintFormatter,则会获得所需的图像视图,然后打印一个空页面
  • 如果有人能帮助我,我将不胜感激

    以下是UIPrintPageRenderer子类中的代码:

    @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
    }
    }