如何仅缩放MKAnnotationView的图片而不缩放CalloutView?
解决MKAnnotationView缩放后CalloutView同步缩放的问题
嘿,这个坑我之前踩过!直接给MKAnnotationView设置transform会把标注弹窗(CalloutView)也一起缩放,毕竟Callout是它的子视图,变换会被子视图继承。咱们换个更稳妥的思路:不要缩放整个AnnotationView,而是提前把公交站牌图片缩放到目标大小,这样就能保证Callout保持正常的可读尺寸了。
修改后的代码示例
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let reuseIdentifier = "stopPin" // 优先复用已有的AnnotationView,提升地图滚动性能 var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier) annotationView?.canShowCallout = true } // 手动缩放原始图片(512x512 → 128x128) if let originalImage = UIImage(named: "busStop.png") { let scaleFactor: CGFloat = 0.25 let targetSize = CGSize( width: originalImage.size.width * scaleFactor, height: originalImage.size.height * scaleFactor ) // 使用UIGraphicsImageRenderer生成缩放后的图片 let renderer = UIGraphicsImageRenderer(size: targetSize) let scaledImage = renderer.image { context in originalImage.draw(in: CGRect(origin: .zero, size: targetSize)) } annotationView?.image = scaledImage } // 移除原来的transform设置,避免影响Callout return annotationView }
为什么这个方法有效?
我们直接修改的是图片本身的像素尺寸,而不是对整个MKAnnotationView应用几何变换。这样CalloutView作为AnnotationView的子视图,不会继承任何缩放变换,自然就能保持默认的正常大小,不会出现难以阅读的问题。
另外,加入AnnotationView的复用逻辑是个小优化,能避免地图滚动时频繁创建新视图,提升整体性能哦~
内容的提问来源于stack exchange,提问作者dinosaysrawr
相关产品推荐
相关产品推荐

