尺寸图像引脚注释

Size image pin annotation

我用个人头像代替了传统的红色别针。当我打开地图显示图钉时,图像覆盖了整个地图。 pin 图片是否有最大尺寸,或者我如何在代码中集成一些东西以适应尺寸标准的经典 pin?

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let annotationIdentifier = "SomeCustomIdentifier" // use something unique that functionally identifies the type of pin

    var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier)

    if annotationView != nil {
        annotationView.annotation = annotation
    } else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)

        annotationView.image = UIImage(named: "pin maps.png")

        annotationView.canShowCallout = true
        annotationView.calloutOffset = CGPointMake(-8, 0)

        annotationView.autoresizesSubviews = true
        annotationView.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView
    }

    return annotationView
}

图钉图片没有最大尺寸限制。您需要调整 UIImage 的大小。

    let annotationIdentifier = "SomeCustomIdentifier"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.canShowCallout = true

        // Resize image
        let pinImage = UIImage(named: "pin maps.png")
        let size = CGSize(width: 50, height: 50)
        UIGraphicsBeginImageContext(size)
        pinImage!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()

        annotationView?.image = resizedImage

        let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
        annotationView?.rightCalloutAccessoryView = rightButton as? UIView
    }
    else {
        annotationView?.annotation = annotation
    }

我知道已经有一个可接受的答案,但它对我不起作用。 Kosuke Ogawa 是正确的,没有最大尺寸,您必须改为调整尺寸。但是,我发现在 MKAnnotationView 上修改 Frame 会产生更好的结果。

Kiko Lobo 评论了最适合我的解决方案,所以一切都归功于他。

无需对 UIImage 进行任何操作,您只需编辑 MKAnnotationView。 Kibo Lobo的评论:

annotationView?.frame.size = CGSize(width: 30, height: 40)

我实际上是在 C# 中使用 Xamarin 完成的,看起来像这样:

annotationView.Frame = new CGRect(0,0,30,40);

接受的答案在 Xamarin 中实施时没有效果。希望这可以帮助其他遇到图像缩放问题的人。 UIImage.Scale() 方法使图像非常模糊,而修改 Frame 保持质量不变。