2013-11-01 42 views
2

我有以下的打印代碼,我希望它打印類連接到視圖控制器的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是完整的工作代碼,從iPad/iPhone的

注意任何UIView的打印位,僅打印的位圖。

需要注意的是(奇怪)的iOS似乎不包括渲染一個UIView到後記的概念.. Print a UIView, but NOT by rendering as a bitmap image

換句話說以下似乎是毫無意義的iOS中,尚未...

UIViewPrintFormatter *f = [self.view viewPrintFormatter]; 

反正下面將打印(就像一個位圖)任何絕對的任何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]; 
    } 

它就是這麼簡單,幸運的。但它是一個渲染的位圖圖像。