Ios Swift-设置注释视图图像的圆角

Ios Swift-设置注释视图图像的圆角,ios,swift,mapkit,mkannotation,mkannotationview,Ios,Swift,Mapkit,Mkannotation,Mkannotationview,这是我的上一个线程,在这里我将图像设置为注释视图 现在我遇到了在注释视图图像上设置圆角的问题 cannot show callout with clipsToBounds enabled swift 似乎我必须在圆角或显示标注之间进行选择,我真的不明白为什么这两个不能出现。有办法吗? 以下是我的viewForAnnotation函数: func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotatio

这是我的上一个线程,在这里我将图像设置为注释视图

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

cannot show callout with clipsToBounds enabled swift
似乎我必须在圆角或显示标注之间进行选择,我真的不明白为什么这两个不能出现。有办法吗? 以下是我的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
}

提前感谢您的帮助

我找到了解决这个问题的方法,我认为它相当优雅

与其更改注释的图层,不如创建UIImageView,将图像附着到其上,并将其作为视图添加到注释中

    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)
让我知道它是否适合你


DZ

再加上dizzie的回答:

添加图像子视图效果良好,但无法再单击pin。 我发现更改pin的大小以匹配图像的大小可以解决此问题

anView.frame = imageView.frame
您可能还需要移动详图索引,具体取决于图像的大小

anView.calloutOffseet = CGPoint(x: 0, y: -10)

另一种方法是添加子层,而不是像这样添加imageView:

let imageLayer = CALayer()
imageLayer.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
imageLayer.contents = image?.cgImage
imageLayer.cornerRadius = imageLayer.frame.size.width / 2
imageLayer.masksToBounds = true
imageLayer.borderWidth = 4.0
imageLayer.borderColor = UIColor.white.cgColor
anView.layer.addSublayer(imageLayer)
anView.frame = imageLayer.frame

看看这个定制的调出演示-我想这是一个有点不同的情况,因为他试图在调出字段内显示图像,我用图像更改pin,没有圆角,一切都很好。