4

我试图找到一个让我使用 AirPrint 打印的功能。

我有一个按钮btnPrint,当按下它时,应该将 myPic.jpg 打印到默认的 AirPrint 设备。但我不知道是否有这样的功能。

我在 xcode 中找不到很多关于 AirPrint 的文档。

4

1 回答 1

4

Apple 提供的打印文档可能会让您受益。

以下是AirPrint 的 Objective-C 代码

检查是否可以打印:

if ([UIPrintInteractionController isPrintingAvailable])
{
    // Available
} else {
    // Not Available
}

单击按钮后打印:

-(IBAction) buttonClicked: (id) sender;
{
    NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@, %@",self.encoded.text, self.decoded.text];
    [printBody appendFormat:@"\n\n\n\nPrinted From *myapp*"];

     UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
     pic.delegate = self;

     UIPrintInfo *printInfo = [UIPrintInfo printInfo];
     printInfo.outputType = UIPrintInfoOutputGeneral;
     printInfo.jobName = self.titleLabel.text;
     pic.printInfo = printInfo;

     UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
     textFormatter.startPage = 0;
     textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
     textFormatter.maximumContentWidth = 6 * 72.0;
     pic.printFormatter = textFormatter;
     [textFormatter release];
     pic.showsPageRange = YES;

     void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
     ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
         if (!completed && error) {
             NSLog(@"Printing could not complete because of error: %@", error);
         }
     };

    [pic presentFromBarButtonItem:self.rightButton animated:YES completionHandler:completionHandler];

}
于 2012-09-07T13:35:19.930 回答