Swift - 将圆角设置为注释视图图像

DCD*_*CDC 3 mapkit mkannotation mkannotationview ios swift

这是我之前的帖子,我将图像设置为注释视图引脚

Swift - 将不同的图像从数组设置为注释引脚

现在我遇到了在注释视图图像上设置圆角的问题.

cannot show callout with clipsToBounds enabled swift
Run Code Online (Sandbox Code Playgroud)

似乎我必须在圆角或显示标注之间做出选择,我真的不明白为什么这两个不能出现.有没有办法做到这一点?这是我的viewForAnnotation函数:

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

    let reuseId = "restaurant"
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.canShowCallout = false
    }
    else {
        anView.annotation = annotation

    }

    let restaurantAnnotation = annotation as RestaurantAnnotation

    if (restaurantAnnotation.image != nil) {

                        anView.image = restaurantAnnotation.image!

                        anView.layer.masksToBounds = true

                        anView.layer.cornerRadius = (anView.frame.size.height)/10.0

    }
    else {
        // Perhaps set some default image
    }

    return anView
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助!

小智 14

我找到了解决方案,我觉得这很优雅.

而不是更改注释的图层,创建一个UIImageView,将图像附加到它,并将其作为视图添加到annoation.

    let imageView = UIImageView(frame: CGRectMake(0, 0, 25, 25))
    imageView.image = UIImage(named: cpa.imageName);
    imageView.layer.cornerRadius = imageView.layer.frame.size.width / 2
    imageView.layer.masksToBounds = true
    anView.addSubview(imageView)
Run Code Online (Sandbox Code Playgroud)

让我知道是否适合您.

DZ