Question

I have the following code for printing, I want it to print the UIVIew of the view controller that the class is attached to,

but printing just generates empty white pages (and two pages instead of one)

I am fairly new to xcode, can you please help spot the mistake?

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];
    }
Was it helpful?

Solution

FWIW here's complete working code to print a bitmap from any UIView from iPad/iPhone

NOTE, THIS ONLY PRINTS A BITMAP.

Note that (bizarrely) iOS seems to NOT INCLUDE the concept of rendering a UIView to postscript .. Print a UIView, but NOT by rendering as a bitmap image

In other words the following seems to be meaningless in iOS, as yet...

UIViewPrintFormatter *f = [self.view viewPrintFormatter];

Anyway the following will print (JUST AS A BITMAP) absolutely any UIView whatsoever...

You simply use renderInContext: or,

for more modern code,

the combination drawViewHierarchyInRect: and 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];
    }

It's really that simple, fortunately. But it is a rendered bitmap image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top