Cocoa 如何在OS X中打印MKMapView?
发送MKMapView a-print:message会产生只包含+/-按钮和合法链接的输出。如果我尝试[NSPrintOperation printOperationWithView:someMKMapView]或[The Window that contains SamapView print]或[[NSPrintOperation PDFOOperationWithView:someMKMapView:someMKMapView.bounds toPath:@foo.pdf printInfo:nil]runOperation]则相同 顺便说一句,苹果自己的Maps.app会打印地图 有人打印过MKMapView吗?魔法类是MKMapSnapshotter 假设有一个MKMapView实例mapView,这是一个简单的示例,可以将MKMapView的当前内容创建为用Swift编写的TIFF文件。 此图像可打印Cocoa 如何在OS X中打印MKMapView?,cocoa,printing,mkmapview,mapkit,nsprintoperation,Cocoa,Printing,Mkmapview,Mapkit,Nsprintoperation,发送MKMapView a-print:message会产生只包含+/-按钮和合法链接的输出。如果我尝试[NSPrintOperation printOperationWithView:someMKMapView]或[The Window that contains SamapView print]或[[NSPrintOperation PDFOOperationWithView:someMKMapView:someMKMapView.bounds toPath:@foo.pdf printIn
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:方法将覆盖或注释坐标值转换到图像坐标空间内的适当位置。请阅读以下内容: