作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在自己学习 Swift 3,我当前的学习项目涉及允许用户拍摄照片并获取固定当前位置的 map 快照。
我依赖 this answer从 2015 年 8 月和 this answer从 2016 年 6 月开始寻求指导,但我仍然找不到正确的路径。
此刻,我可以...
let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)
snapShotter.start() {
snapshot, error in
guard let snapshot = snapshot else {
return
}
let image = snapshot.image
let annotation = MKPointAnnotation()
annotation.coordinate = needleLocation // is a CLLocationCoordinate2D
annotation.title = "My Title"
let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
// I want to push the final image to a global variable
// can't figure out how to define finalImage as the pinned map
self.myMap = finalImage
} // close snapShotter.start
最佳答案
渲染 MKMapSnapshot
使用注释 View ,您必须手动绘制快照的 image
和注释 View 的 image
在新的图形上下文中,并从该上下文中获取新图像。在 Swift 3 中:
let rect = imageView.bounds
let snapshot = MKMapSnapshotter(options: options)
snapshot.start { snapshot, error in
guard let snapshot = snapshot, error == nil else {
print(error ?? "Unknown error")
return
}
let image = UIGraphicsImageRenderer(size: options.size).image { _ in
snapshot.image.draw(at: .zero)
let pinView = MKPinAnnotationView(annotation: nil, reuseIdentifier: nil)
let pinImage = pinView.image
var point = snapshot.point(for: location.coordinate)
if rect.contains(point) {
point.x -= pinView.bounds.width / 2
point.y -= pinView.bounds.height / 2
point.x += pinView.centerOffset.x
point.y += pinView.centerOffset.y
pinImage?.draw(at: point)
}
}
// do whatever you want with this image, e.g.
DispatchQueue.main.async {
imageView.image = image
}
}
.satelliteFlyover
的快照示例:
关于swift3 - Swift 3 向 MKMapSnapShotter 快照添加自定义注释引脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42757185/
我是一名优秀的程序员,十分优秀!