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