打印UIView,但不要将其渲染为位图图像 [英] Print a UIView, but NOT by rendering as a bitmap image

查看:139
本文介绍了打印UIView,但不要将其渲染为位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个简单的UIView,只有文本(即UILabel),可能还有一些黑线。

Say you have a simple UIView with only text (ie, UILabel) and maybe some black lines.

以下是您打印UIView的确切方法......

Here's exactly how you can print that UIView...

将其渲染为UIImage,然后打印出来......

render it as a UIImage, and print that...

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

但是这会给你废话结果。打印排版是一种荒谬的方式。它应该作为postscript或者其他东西发送。

But this gives you crap results. It's a ridiculous way to print typography. It should be being sent as postscript, or something.

在iPad屏幕上...

on iPad screen...

打印时..

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

Note that, of course, if you use a retina ipad it's a bit better, but that's not the point. it's ridiculous to print text information, black lines, as an image.

现在,您认为可以打印出像这样的UIView ......

Now, you'd think you could print a UIView something like this ...

pic.printFormatter = [someUIView viewPrintFormatter];

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

but I can't get that to work no matter what I try.

有没有人对此有任何意见?谢谢!

Does anyone have anything on this? Thanks!

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

PS - note that in iOS, we've recently changed from using renderInContext to using the snapshot call. It makes absolutely no difference to the issue here, cheers.

推荐答案

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

As Apple Doc states, viewPrintFormatter attribute of UIView is defined in a UIView category but it's available only for instances of type: UITextView, MKMapView and UIWebView.

所以你可以:

  • print UIView using it's layer attribute and flattening all its content
  • initialize a UIWebView with the content to print and giving its viewPrintFormatter attribute to a pageRenderer and render each page

这篇关于打印UIView,但不要将其渲染为位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆