[ios]air印刷的 UIView 生成白页

标签: Xcode ios
发布时间: 2017/3/27 20:14:32
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有下面的代码用于印刷,我想要打印的视图控制器类附加到 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:

苍天在这里是完整的工作代码,用于从任何 UIView 从 iPad 或 iPhone 打印位图

请注意,这只是打印的位图。

值得注意的是,(广州站) iOS 似乎不包括渲染到 postscript UIView 的概念。打印的 UIView,但不呈现为位图图像

换句话说以下似乎是毫无意义的在 iOS 中,还...

UIViewPrintFormatter *f = [self.view viewPrintFormatter];

反正以下将打印 (只是作为位图) 绝对任何 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];
    }

幸运的是,它是真的很简单。 但它是一个呈现的位图图像。

官方微信
官方QQ群
31647020