如何使UIImage圆形

时间:2017-06-03 23:36:43

标签: ios swift uiimage mapkit mkannotation

我正在尝试使annotation.image圆。但我不知道怎么做。我将如何进行UIImage巡回赛。

 //Customize Annotation
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    let annotationIdentifier = "Identifier"

    var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)

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


        annotationView.image = profileImage.image

        annotationView.canShowCallout = true
        annotationView.frame = CGRect(x: 0, y: 0, width: 25, height: 25)

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


      return annotationView
}

}

Currently looks like this on a map

4 个答案:

答案 0 :(得分:0)

您应该使用cornerRadius属性。 它将类似于:

annotationView.layer.cornerRadius = yourCornerRadius

如果您将yourCornerRadius设置为annotationView的高度或宽度的一半,则应获得一轮annotationView

如果这不起作用,也可以添加此行

annotationView.layer.maskToBounds = true

答案 1 :(得分:0)

添加imageView怎么样?

let imageView = UIImageView(frame: CGRectMake(0, 0, 25, 25))
imageView.image = UIImage(named: "image.png");
imageView.layer.cornerRadius = imageView.layer.bounds.size.width / 2
imageView.layer.masksToBounds = true
annotationView.addSubview(imageView)

答案 2 :(得分:0)

试试这个方法:

func getRoundedImage(originalImage: UIImage,view: UIView,radius:CGFloat,borderWidth:CGFloat) -> UIImage?{
        UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0)
        let path = UIBezierPath(roundedRect: view.bounds.insetBy(dx: borderWidth / 2, dy: borderWidth / 2), cornerRadius: radius)
        let context = UIGraphicsGetCurrentContext()
        context!.saveGState()
        path.addClip()
        originalImage.draw(in: view.bounds)
        UIColor.gray.setStroke()
        path.lineWidth = borderWidth
        path.stroke()
        let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return roundedImage

    }

答案 3 :(得分:0)

要将正方形设为圆形,只需将其圆角半径设置为其宽度/高度的一半即可。

imageView.layer.cornerRadius = 12.5 //Half the sideLength of the imageView
imageView.layer.masksToBounds = true