gpt4 book ai didi

iphone - 如何使用 AirPrint 打印 NSString?

转载 作者:行者123 更新时间:2023-12-03 18:54:41 24 4
gpt4 key购买 nike

我在这方面遇到了很大的麻烦。我不知道出了什么问题,但它吐出了这个:

错误:尝试在未设置打印源(item/items/formatter/renderer)的情况下显示打印选项

如果我将其设置为打印 png 图像,但不打印 NSString 的内容,则该代码可以工作。

这是我的代码:

- (IBAction)pushPrint:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:@"dd/MM/YY HH:mm:ss"];
NSString* currentTime = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

NSString *path = [[NSBundle mainBundle] pathForResource:t[NSString stringWithFormat:@"Results Printout\n"
"\n"
"%@\n"
"\n"
"\n"
"%@", currentTime, pasteboard.string] ofType:nil];

NSData *myData = [NSData dataWithContentsOfFile:path];
UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];

print.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
print.printInfo = printInfo;
print.showsPageRange = YES;
print.printingItem = myData;

void (^completionHandler)(UIPrintInteractionController *,BOOL, NSError *) = ^(UIPrintInteractionController *print,BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"houston we have a problem");
}
};
[print presentAnimated:YES completionHandler:completionHandler];
}

最佳答案

您似乎想要打印一个普通的 NSString,但您正在尝试加载一个文件路径无效的文件。因此,myData很可能是nil(请与调试器确认),因此错误消息是正确的(print.printingItem不能是)。

要打印简单文本,请创建 UISimpleTextPrintFormatter 的实例(使用 initWithText:)并将该实例分配给 print.printFormatter

关于iphone - 如何使用 AirPrint 打印 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5068322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com