ios - 打印 UIView,但不是通过渲染为位图图像

标签 ios printing uiprintinfo

假设您有一个简单的 UIView,其中只有文本(即 UILabel)和一些黑线。

enter image description here

下面是打印 UIView 的具体方法...

将其呈现为 UIImage,然后打印...

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

但这会给您带来糟糕的结果。这是一种可笑的排版打印方式。它应该作为后记之类的形式发送。

在 iPad 屏幕上...

enter image description here

打印时..

enter image description here

请注意,当然,如果您使用视网膜 ipad 会好一点,但这不是重点。将文本信息、黑线打印为图像是荒谬的。

现在,您认为您可以像这样打印一个 UIView ...

pic.printFormatter = [someUIView viewPrintFormatter];

但无论我尝试什么,我都无法让它工作。

有人知道吗?谢谢!

PS - 请注意,在 iOS 中,我们最近从使用 renderInContext 更改为使用快照调用。这对这里的问题完全没有影响,干杯。

最佳答案

作为Apple Doc声明,UIView 的 viewPrintFormatter 属性在 UIView 类别中定义,但它仅适用于以下类型的实例:UITextViewMKMapViewUIWebView

所以你可以:

关于ios - 打印 UIView,但不是通过渲染为位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24479985/

相关文章:

ios - 如何在 IOS Storyboard 中单击时更改部分布局

javascript - alert() 在 WKWebview 中不起作用 evaluateJavaScript()

html - 如何在javascript中找出打印纸的维度

C++ cout 不会打印

swift - 可用打印机及其 URL 的日志列表

ios - 有什么方法可以持久化 Swift 闭包吗?

javascript - iOS Safari 上 javascript 中的地理位置

c - 从单链表打印结构数据

ios - 列出可访问的 AirPrint 打印机