所以我有许多巴士站在地图上标记为注释。我想使用公共汽车站牌的图像,而不是默认的红色气泡或图钉,所以我成功地更改了图像。
在这样做时,由于图像是 512x512,我决定使用变换缩放 MKAnnotationView。因此,当我选择公交车站时,标注气泡现在也会缩放到相同的级别,这使得它不可读。
有什么方法可以在不缩放标注视图的情况下缩放图像?
我的代码:
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
}
使用 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