【问题标题】:MKAnnotationView scaling image causes calloutView to scale as wellMKAnnotationView 缩放图像导致 calloutView 也缩放
【发布时间】:2018-01-26 11:02:18
【问题描述】:

所以我有许多在地图上标记为注释的巴士站。我想使用公共汽车站标志的图像,而不是默认的红色气泡或图钉,因此我成功更改了图像。

这样做时,因为图像是 512x512,我决定使用变换来缩放 MKAnnotationView。因此,当我选择一个公共汽车站时,标注气泡现在也被缩放到相同的级别,这使得它不可读。

有什么方法可以在不缩放 calloutView 的情况下缩放图像?

我的代码:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "stopPin")
    annotationView.canShowCallout = true
    annotationView.image = UIImage(named: "busStop.png")
    let transform = CGAffineTransform(scaleX: 0.25, y: 0.25)
    annotationView.transform = transform
    return annotationView
}

【问题讨论】:

    标签: swift swift3 mapkit mkannotation mkannotationview


    【解决方案1】:

    使用UIGraphics

    let image = UIImage(named: "busStop.png")
    let resizedSize = CGSize(width: 100, height: 100)
    
    UIGraphicsBeginImageContext(resizedSize)
    image?.draw(in: CGRect(origin: .zero, size: resizedSize))
    let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    annotationView?.image = resizedImage
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 2012-04-06
      • 2019-09-17
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多