将图像发送到AirPrint的功能

时间:2012-09-07 13:30:56

标签: objective-c ios airprint

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

我有一个按钮btnPrint,按下后,应将myPic.jpg打印到默认的AirPrint设备。但我无法弄清楚是否有这样的功能。

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

1 个答案:

答案 0 :(得分:4)

Apple有documentation on printing可能会对您有所帮助。

以下内容来自Objective-C code for AirPrint

检查是否可以打印:

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

}