2

发送消息会导致仅包含 +/- 按钮和“合法”链接的输出MKMapView-print:如果我尝试[NSPrintOperation printOperationWithView:someMKMapView]or [theWindowThatContainsAMapView print]or也是一样[[NSPrintOperation PDFOperationWithView:someMKMapView insideRect:someMKMapView.bounds toPath:@"foo.pdf" printInfo:nil] runOperation]的。

苹果自己的 Maps.app 确实打印了地图,顺便说一句。

有没有人设法打印 MKMapView?

4

1 回答 1

4

魔术课是MKMapSnapshotter

假设有一个MKMapView实例mapView,这是一个简单的示例,用于将 MKMapView 的当前内容的图像创建为用 Swift 编写的 TIFF 文件。此图像是可打印的。

let options = MKMapSnapshotOptions()
options.region = mapView.region;
options.size = mapView.frame.size;

let fileURL = NSURL(fileURLWithPath:"/path/to/snapshot.tif")
let mapSnapshotter = MKMapSnapshotter(options: options)
mapSnapshotter.startWithCompletionHandler { (snapshot, error) -> Void in
  // do error handling
  let image = snapshot.image
  if let data = image.TIFFRepresentation {
    data.writeToURL(fileURL!, atomically:true)
  } else {
    println("could not create TIFF data")
  }
}

编辑:

打印而不是创建文件

let options = MKMapSnapshotOptions()
options.region = mapView.region;
options.size = mapView.frame.size;

let mapSnapshotter = MKMapSnapshotter(options: options)
mapSnapshotter.startWithCompletionHandler { (snapshot, error) -> Void in
  // do error handling
  let image = snapshot.image
  let imageView = NSImageView()
  imageView.frame = NSRect(origin: NSZeroPoint, size: image.size)
  imageView.image = image

  let info = NSPrintInfo.sharedPrintInfo()
  info.horizontalPagination = .FitPagination
  info.verticalPagination = .FitPagination
  let operation = NSPrintOperation(view: imageView, printInfo:info)
  operation.showsPrintPanel = true
  operation.runOperation()
于 2015-07-02T13:30:04.107 回答