大小图像引脚注释

Ric*_*oli 11 mapkit mkannotation ios xcode7 swift2

我把个人形象改为传统的红色别针.当我打开地图以显示图钉时,图像会覆盖整个地图.是否有最大尺寸的引脚图像或如何在代码中集成某些东西以适应尺寸标准的经典引脚?

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
}
Run Code Online (Sandbox Code Playgroud)

Kos*_*awa 28

没有最大尺寸的引脚图像.你需要调整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
    }
Run Code Online (Sandbox Code Playgroud)

  • 我会使用:anView?.frame.size = CGSize(width:30,height:40)而不是所有drawContext的东西 (4认同)

小智 6

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

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

您只需编辑 MKAnnotationView,而不用对 UIImage 做任何事情。Kibo Lobo 的评论:

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

我实际上用 Xamarin 在 C# 中做了这个,看起来像这样:

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

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