【发布时间】:2016-02-12 14:31:13
【问题描述】:
我正在尝试从 UITextView 打印一些文本,以及从 UIImageView 打印一张图片。我在网上搜索了一些教程,我成功地从 UITextView 打印了文本,但我还没有找到用它打印图像的方法。 我要打印的 UIImageView 是 qrImageView 而 UITextView 叫做 qrNoteTextView。 我使用以下代码打印 TextView:
- (IBAction)qrPrint:(id)sender {
NSLog(@"print action");
[self.qrNoteTextView resignFirstResponder];
NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@ \n %@", self.qrNoteTextView.text, self.qrImageView.image];
[printBody appendFormat:@"\nCreated using ..."];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = self.qrNoteTextView.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;
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.qrNoteTextView animated:YES completionHandler:completionHandler];
}
感谢您的宝贵时间!
【问题讨论】:
-
将 UITextView 内容转换为 NSData 并尝试使用 UTF8 格式编码后打印。愿它对你有所帮助。
标签: ios objective-c uiimageview uiimage airprint