1

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

enter image description here

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

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

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

on iPad screen...

enter image description here

when printed ..

enter image description here

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.

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 - 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.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Why would you think there is formatter for this and what exactly is you are trying to achieve? Anyway how about creating a PDF file? http://stackoverflow.com/questions/5443166/how-to-convert-uiview-to-pdf-within-ios – Matic Oblak Jun 30 '14 at 06:54
  • I think things like PDFs are your best shot at the moment but you need to use things like paths since it uses vectors and does not lose resolution while scaled. I am pretty sure this is possible but can be quite hard getting any view type to draw as PDF element. The thing is iOS uses some internal protocol to draw views on the context which is much similar to PDF procedure, I even remember someone once found a way to import PS layers into iOS so all lines, gradients etc were drawn exactly as in PS. – Matic Oblak Jun 30 '14 at 13:48
  • This "format" in iOS is in sense written with classes such as UIView, each of the class has some extra parameters which are used to describe how to draw them (no resolution lost). It might be that the interface builder has some specific format which you are looking for (though I doubt it since it needs no portability at all) but even if it does there are no other applications that can read it but Xcode. – Matic Oblak Jun 30 '14 at 13:54
  • Right that's what I'm asking, hopefully someone with deep insider knowledge of this will show up. Cheers for now. – Fattie Jun 30 '14 at 13:54
  • I know only of things that do exist, I can not be certain such a thing does not exist as I might simply not have heard of if :) – Matic Oblak Jun 30 '14 at 13:55

1 Answers1

1

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.

So you can either:

Community
  • 1
  • 1
Biasu
  • 439
  • 10
  • 19