MKAnnotationView缩放图像会导致calloutView也缩放

3

我有很多公交车站标记在地图上。我想要使用公交车站标志的图像而不是默认的红色气泡或针,所以我成功地更改了图像。

在这样做时,因为图像大小为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
}
1个回答

3

使用 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接