【问题标题】:How do I print a MKMapView in OS X?如何在 OS X 中打印 MKMapView?
【发布时间】:2014-08-28 21:30:57
【问题描述】:

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

Apple 自己的 Maps.app 确实会打印地图,顺便说一句。

有人设法打印出 MKMapView 吗?

【问题讨论】:

  • 你有想过这个吗?

标签: cocoa printing mkmapview mapkit nsprintoperation


【解决方案1】:

魔术班是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()

【讨论】:

  • 值得注意的是,来自文档:“快照图像不包括您的应用添加到地图视图中的任何自定义叠加层或注释。如果您希望您的注释和叠加层出现在最终图像上,您必须自己绘制它们。要在图像上正确定位这些项目,请使用该类的 pointForCoordinate: 方法将覆盖或注释坐标值转换到图像坐标空间内的适当位置。"
猜你喜欢
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
相关资源
最近更新 更多