UIView的Air Printing生成白页

时间:2013-11-01 10:34:24

标签: ios xcode airprint

我有以下代码用于打印,我希望它打印该类附加到的视图控制器的UIVIew,

但是打印只生成空白页(和两页而不是一页)

我对xcode很新,请你帮忙发现错误?

UIPrintInteractionController *pc = [UIPrintInteractionController
                                        sharedPrintController];
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"Print file";
    pc.printInfo = printInfo;
    UIViewPrintFormatter *Pformatter = [self.view viewPrintFormatter];
    pc.printFormatter = Pformatter;

    UIPrintInteractionCompletionHandler completionHandler =
    ^(UIPrintInteractionController *printController, BOOL completed,
      NSError *error) {
        if(!completed && error){
            NSLog(@"Print failed - domain: %@ error code %u", error.domain,
                  error.code);
        }
    };
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [pc presentFromBarButtonItem:self.btnPrint animated:YES
                   completionHandler:completionHandler];
    } else {
        [pc presentAnimated:YES completionHandler:completionHandler];
    }

1 个答案:

答案 0 :(得分:4)

FWIW这里是完整的工作代码,用于从iPad / iPhone的任何UIView打印位图

注意,这只会打造一个BITMAP。

请注意(奇怪)iOS似乎并未包含将UIView呈现为postscript的概念.. Print a UIView, but NOT by rendering as a bitmap image

换句话说,iOS中的以下内容似乎毫无意义......

UIViewPrintFormatter *f = [self.view viewPrintFormatter];

无论如何,以下将打印(只是作为一个BITMAP)绝对任何UIView ...

您只需使用 renderInContext:

了解更多现代代码,

组合drawViewHierarchyInRect:和UIGraphicsGetImageFromCurrentImageContext()

- (IBAction)printB:(id)sender
    {
    // we want to print a normal view ... some UILabels, maybe a black line

    // in this technique, depressingly we CREATE AN IMAGE of the view...

    // step 1. make a UIImage, of the whole view.

    UIGraphicsBeginImageContextWithOptions(self.printMe.bounds.size, NO, 0.0);

    // [self.printMe.layer renderInContext:UIGraphicsGetCurrentContext()];
    // UIImage *asAnImage = UIGraphicsGetImageFromCurrentImageContext();
    // .... or, more futuristically.....
    [self.printMe drawViewHierarchyInRect:self.printMe.bounds
        afterScreenUpdates:NO];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // step 2. choose grayscale, etc

    UIPrintInfo *info = [UIPrintInfo printInfo];
    info.orientation = UIPrintInfoOrientationPortrait;
    info.outputType = UIPrintInfoOutputGrayscale;

    // step 3, print that UIImage

    UIPrintInteractionController *pic =
       [UIPrintInteractionController sharedPrintController];
    pic.delegate = self;
    //pic.printingItem = asAnImage;
    pic.printingItem = snapshotImage;
    pic.printInfo = info;

    UIPrintInteractionCompletionHandler completionHandler =
    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
        {
        if (error)
            NSLog(@"failed... %@ %ld", error.domain, (long)error.code);
        if (completed)
            NSLog(@"completed yes");
        else
            NSLog(@"completed no");
        };     

    [pic presentAnimated:YES completionHandler:completionHandler];
    }

幸运的是,这真的很简单。但它是一个渲染的位图图像。