【问题标题】:How to make UIImage round [duplicate]如何使UIImage圆形[重复]
【发布时间】:2017-06-03 23:36:42
【问题描述】:

我正在尝试使 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

【问题讨论】:

    标签: ios swift uiimage mapkit mkannotation


    【解决方案1】:

    您应该使用cornerRadius 属性。 它会是这样的:

    annotationView.layer.cornerRadius = yourCornerRadius
    

    如果您将yourCornerRadius 设置为annotationView 的一半高度或宽度,您应该得到一个圆形annotationView

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

    annotationView.layer.maskToBounds = true
    

    【讨论】:

    • 试过了。但是当我将 maskToBound 设置为 true 时出现此错误 --> clipsToBounds = YES; layer = > 无法显示启用 clipsToBounds 的标注' *** 第一次抛出调用堆栈:
    【解决方案2】:

    添加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)
    

    【讨论】:

    • 我会为 UIImage(named: ) ... 放什么?我
    • @JoseMelendez 这里可以是profileImage.image
    • 我没有收到任何错误,但它仍然无法正常工作。图片仍然是方形的。
    • 您的解决方案对我帮助最大。我做错的是我将annotationView.image 设置为profile.image。我所要做的就是删除它。谢谢!
    【解决方案3】:

    试试这个方法:

    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
    
        }
    

    【讨论】:

    • 视图参数应该放什么?
    • 你可以试试这条线吗? -- annotationView.image = getRoundedImage(originalImage: profileImage.image, view: annotationView, radius: self.annotationView.frame.width/2, borderWidth: 1.0) -- 代替annotationView.image = profileImage.image
    • 当它在函数中解开这个上下文时发现 nil!.saveGState()。
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-17
      • 2011-09-25
      • 1970-01-01
      • 2019-10-17
      • 2011-11-15
      • 2020-05-05
      • 2011-09-19
      • 2014-07-11
      • 1970-01-01
      相关资源
      最近更新 更多