Iphone AirPrint的Objective-C代码

Iphone AirPrint的Objective-C代码,iphone,cocoa-touch,printing,Iphone,Cocoa Touch,Printing,如何使用iAction方法在objective-c中使用AirPrint打印UITextView?检查打印是否可用: if ([UIPrintInteractionController isPrintingAvailable]) { // Available } else { // Not Available } 单击按钮后打印: -(IBAction) buttonClicked: (id) sender; { NSMutableString *printBody =

如何使用iAction方法在objective-c中使用AirPrint打印UITextView?

检查打印是否可用:

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

}

最初由“87vert”发布于:

以下方法使用要打印的文件名以及要显示airprint弹出窗口的条形码。 这对我很有效,我肯定会有帮助的

-(void)printJob:(int)jobType:(NSString*)jobName:(UIBarButtonItem *)barButton{

NSString *path;
if ([jobName isEqualToString:@"Printout.png"]) {
    path= [self documentsPathForFileName:@"Printout.png"];
}

NSData *mydata=[NSData dataWithContentsOfFile:path];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = mydata;
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:barButton animated:YES completionHandler:completionHandler];

}

它工作正常,请确保您正确地实现了它。查看“twerner”在iPhoneDevSDK上发布的另一个示例(链接到回答中提供的特定线程)。这很好。我不打算在我的应用程序中实现AirPrinting,但我只是在本教程中尝试了一下,大概10分钟就完成了。太棒了。谢谢,“编码文本”和“解码文本”到底是什么?!这很愚蠢-它只是从UITextView打印未格式化的文本字符串。至少,您可以将其打印为渲染位图(更容易)。。如果需要,您需要将UIPrintInteractionControllerDelegate添加到标头以接收来自对话框的消息。