OStack程序员社区-中国程序员成长平台

标题: ios - UIView 的空中打印生成白页 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:20
标题: ios - UIView 的空中打印生成白页

我有以下打印代码,我希望它打印该类所附加到的 View Controller 的 UIVIew,

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

我是 xcode 的新手,你能帮忙找出错误吗?

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

    UIPrintInteractionCompletionHandler completionHandler =
    ^(UIPrintInteractionController *printController, BOOL completed,
      NSError *error) {
        if(!completed && error){
            NSLog(@"rint 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];
    }



Best Answer-推荐答案


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

注意,这只会打印位图。

请注意,(奇怪的是)iOS 似乎不包括将 UIView 渲染到 postscript 的概念.. Print a UIView, but NOT by rendering as a bitmap image

换句话说,以下内容在 iOS 中似乎还没有意义......

UIViewPrintFormatter *f = [self.view viewPrintFormatter];

无论如何,以下内容将打印(仅作为位图)绝对任何 UIView...

您只需使用 renderInContext: 或,

对于更现代的代码,

drawViewHierarchyInRect: 和 UIGraphicsGetImageFromCurrentImageContext() 的组合

- (IBAction)printBid)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];
    }

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

关于ios - UIView 的空中打印生成白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19725033/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4