gpt4 book ai didi

swift - MKMarkerAnnotationView 标题未绘制在 MKSnapShot 图像上

转载 作者:行者123 更新时间:2023-11-30 11:42:55 30 4
gpt4 key购买 nike

我尝试在带有标题的图像 (MKSnapshot) 上绘制标记,但标题未显示。
有谁知道为什么会出现这种情况吗?

let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "TitleTest"
let pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "test")
pinView.titleVisibility = MKFeatureVisibility.visible
pinView.dragState = .none
pinView.animatesWhenAdded = false
pinView.canShowCallout = false
pinView.titleVisibility = .visible
pinView.contentMode = .scaleAspectFit
pinView.bounds = CGRect(x: 0, y: 0, width: 40, height: 40)
if rect!.contains(point) {
let pinCenterOffset = pinView.centerOffset
point.x -= pinView.bounds.size.width / 2
point.y -= pinView.bounds.size.height / 2
point.x += pinCenterOffset.x
point.y += pinCenterOffset.y
}
pinView.drawHierarchy(in: CGRect(
x:point.x,
y:point.y,
width:pinView.bounds.width,
height:pinView.bounds.height),
afterScreenUpdates: true)
print("Draw a marker on iOS 11")

这是我的 UIImageView 的屏幕截图,带有标记,但没有标题:

Screenshot of my UIImageView with marker but without title

最佳答案

标题可以绘制在图钉位置下方的快照上:

  • 确定注释在图像坐标系中的位置snapshot.point(for:annotation.coordinate)

  • 创建标题文本的属性,您可以使其类似于 Apple 使用的属性:

    private func titleAttributes() -> [NSAttributedStringKey: NSObject] {
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    let titleFont = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.semibold)
    let attrs = [NSAttributedStringKey.font: titleFont,
    NSAttributedStringKey.paragraphStyle: paragraphStyle]
    return attrs
    }
  • 将其绘制在注释位置的正下方:

    private func drawTitle(title: String,
    at point: CGPoint,
    attributes: [NSAttributedStringKey: NSObject]) {
    let titleSize = title.size(withAttributes: attributes)
    title.draw(with: CGRect(
    x: point.x - titleSize.width / 2.0,
    y: point.y + 1,
    width: titleSize.width,
    height: titleSize.height),
    options: .usesLineFragmentOrigin,
    attributes: attributes,
    context: nil)
    }

有关包含代码和屏幕截图的更完整示例,请参阅 my following answer (它绘制了一个自定义引脚图像,但其余代码可以在此处使用)。

关于swift - MKMarkerAnnotationView 标题未绘制在 MKSnapShot 图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49138443/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com