2013-11-01 1 views
2

Я следующий код для печати, я хочу, чтобы напечатать UIView контроллера представления о том, что класс прикреплен к,Air Печать из UIView создает белые страницы

но печать просто генерирует пустые белые страницы (и две страницы вместо одной)

Я довольно новичок в xcode, не могли бы вы помочь обнаружить ошибку?

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

ответ

4

FWIW здесь полный рабочий код для печати растрового изображения из любого UIView от IPad/iPhone

ПРИМЕЧАНИЯ ЭТО ТОЛЬКО распечатывает Bitmap.

Обратите внимание, что (причудливо) IOS, кажется, не включают в себя концепцию рендеринга UIView в POSTSCRIPT .. Print a UIView, but NOT by rendering as a bitmap image

Других слов, после, кажется, не имеет смысла в прошивке, пока ...

UIViewPrintFormatter *f = [self.view viewPrintFormatter]; 

В любом случае следующий напечатает (JUST как растровое) абсолютно любой UIView бы то ни было ...

Вы просто использовать renderInContext: или

для более современного кода,

сочетание drawViewHierarchyInRect: и 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]; 
    } 

Это действительно так просто, к счастью. Но это рендеринг растрового изображения.