【问题标题】:Size image pin annotation尺寸图像引脚注释
【发布时间】:2015-10-21 15:01:05
【问题描述】:

我放的是个人形象,而不是传统的红色别针。当我打开地图显示图钉时,图像覆盖了整个地图。是否有图钉图像的最大尺寸,或者我如何在代码中集成某些内容以适应尺寸标准经典图钉?

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
}

【问题讨论】:

    标签: ios mapkit swift2 xcode7 mkannotation


    【解决方案1】:

    图钉图像没有最大尺寸。你需要调整 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
        }
    

    【讨论】:

    • 它说 CGRectMake 不在 swft 中
    • pinImage.draw(in: CGRect(x:0, y:0, width:size.width, height:size.height))
    • 我会使用:anView?.frame.size = CGSize(width: 30, height: 40) 而不是所有的 drawContext 东西
    【解决方案2】:

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

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

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

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

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

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

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

    【讨论】:

    • 这是一个很好的答案,它在最新的 iOS15 上也非常好用!在 SwiftUI UIViewRepresentable 中使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2011-01-06
    • 2018-05-30
    相关资源
    最近更新 更多