1

我正在尝试用 Swift 打印。我看过 2014 年 WWDC Adopting AirPrint 视频,这使得它在 Obj-C 中变得非常简单,但我真的很想在 Swift 中完成这项工作。文档表明您必须使用字典创建 UIPrintInfo 对象。很好,但是如何创建 UIPrintInfo 需要的字典呢?具体来说,您使用什么键?我查看了文档,但我不清楚密钥需要是什么。

4

2 回答 2

3

可以使用字典来创建UIPrintInfo实例,但似乎缺少使这变得容易的常量。更简单的是用nil字典创建一个对象,然后设置属性。完成后,您可以检索和检查字典表示。

let printInfo = UIPrintInfo(dictionary: nil)
printInfo.printerID = savedPrinterID
printInfo.jobName = "My Print Job"
printInfo.duplex = UIPrintInfoDuplex.ShortEdge
printInfo.orientation = UIPrintInfoOrientation.Landscape
printInfo.outputType = UIPrintInfoOutputType.Photo

if let infoDict = printInfo.dictionaryRepresentation() {
    println(infoDict)
    // [UIPrintInfoOrientationKey: 1, UIPrintInfoJobNameKey: My Print Job,
    // UIPrintInfoPrinterIDKey: ABC123, UIPrintInfoOutputTypeKey: 1, 
    // UIPrintInfoDuplexKey: 2]
    let anotherPrintInfo = UIPrintInfo(dictionary: infoDict)
}
于 2014-11-04T01:01:16.163 回答
1

想了想,我通过创建一个Obj-C项目,像示例项目一样创建UIPrintInfo对象,然后在UIPrintInfo对象上调用dictionaryRepresentation(返回我们需要的字典)找到了答案。

UIPrintInfo *printInfo=[UIPrintInfo printInfo];
printInfo.outputType=UIPrintInfoOutputGrayscale;
printInfo.jobName=@"Print Job 1";
printInfo.orientation=UIPrintInfoOrientationLandscape;
printInfo.printerID=@"Printer ID Here";
NSDictionary *dict=[printInfo dictionaryRepresentation];

所以这里是关键:

UIPrintInfoDuplexKey

UIPrintInfoOrientationKey

UIPrintInfoOutputTypeKey

UIPrintInfoPrinterIDKey

UIPrintInfoJobNameKey

前三个键具有文档中列出的枚举的 int 值。最后两个键具有您自己选择的字符串值。

希望这可以帮助其他人。

于 2014-11-04T01:09:41.523 回答