2014-10-17 30 views
1

我想展示一个UIPrintInteractionController的实例来使用AirPrint来打印图像。起初,它好像什么都没有发生,但在委托方法printInteractionControllerDidPresentPrinterOptions:内部,我使用self.presentedViewController来获取有关假定的“呈现”打印交互控制器的信息。发现它的宽度为19.000,高度为0.000 ...似乎没有办法在实际显示之前访问视图控制器,因为它是在框架中自动生成的,而且我还没有能够成功修改其框架展示后。其他人遇到此问题和/或找到解决办法?iOS 8打印交互控制器在iPad上有0高度

- (void)printBadgeImage:(UIImage *)image { 

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; 

    UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
    printInfo.outputType = UIPrintInfoOutputGeneral; 
    printInfo.jobName = BADGE_PRINT_JOB; 
    printInfo.duplex = UIPrintInfoDuplexLongEdge; 
    pic.printInfo = printInfo; 
    pic.printingItem = image; 
    pic.delegate = self; 

    UIPrintInteractionCompletionHandler completionHandler = 
    ^(UIPrintInteractionController *printController, 
     BOOL completed, 
     NSError *error) { 

     if (completed) { 

      NSLog(@"Print job completed"); 

     } else if (!completed && error) { 

      NSLog(@"Print job error"); 
      NSString *errorString = [NSString stringWithFormat:@"An error occured while printing: %@", error]; 
      UIAlertView *alert = [[DOSimpleAlertView alloc] initWithTitle:@"Error" 
                    message:errorString]; 

      [alert show]; 

     } else { 

      NSLog(@"Print job incomplete without error"); 
     } 
    }; 

    [pic presentFromRect:self.view.frame 
        inView:self.view 
       animated:YES 
     completionHandler:completionHandler]; 

} 

- (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController { 

    NSLog(@"Did present"); 
    // following printed <UIPrintInteractionController: 0x145d1a30> 
    NSLog(@"The Print Interaction Controller: %@", printInteractionController); 
    // following printed <UINavigationController: 0x145d3840> 
    NSLog(@"The presented view: %@", self.presentedViewController); 
    UIViewController *presented = self.presentedViewController; 
    // following printed 19.000000 
    NSLog(@"Presented view width: %f", presented.view.frame.size.width); 
    // following printed 0.000000 
    NSLog(@"Presented view height: %f", presented.view.frame.size.height); 
    // following printed 0.000000 
    NSLog(@"Presented view x: %f", presented.view.frame.origin.x); 
    // following printed 0.000000 
    NSLog(@"Presented view y: %f", presented.view.frame.origin.y); 

回答

0

我有一个打印图像的应用程序,但我没有看过长时间的代码。但是,这里是简短的,希望它有帮助。

NSData *moneyData = UIImageJPEGRepresentation(image, 1.0); 
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; 
if(pic && [UIPrintInteractionController canPrintData: moneyData]) { 

    pic.delegate = self; 

    UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
    printInfo.outputType = UIPrintInfoOutputGeneral; 
    printInfo.jobName = @"MONEY"; 
    printInfo.duplex = UIPrintInfoDuplexLongEdge; 
    pic.printInfo = printInfo; 
    pic.showsPageRange = YES; 
    pic.printingItem = moneyData; 

    [pic presentFromBarButtonItem:self.printButton animated:YES completionHandler:nil]; 
} 

如果我没有记错的话,那么关于如何呈现它,有一些特别之处。你可能想尝试从我的酒吧按钮项目。