2017-06-03 65 views
1

我正在尝试制作annotation.image。但我不知道如何。我将如何去做一个UIImage轮。如何制作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

回答

0

你应该玩的cornerRadius财产。 这将是这样的:

annotationView.layer.cornerRadius = yourCornerRadius 

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

如果没有作品加入这一行太

annotationView.layer.maskToBounds = true 
+0

试过了。但是,当我将maskToBound设置为true时 - > clipToBounds = YES;图层= >无法显示调用clipToBounds启用' ***第一次抛出调用堆栈: – JoseMelendez

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) 
+0

我会为UIImage(命名为:)...做什么? I – JoseMelendez

+0

@JoseMelendez它可以'profileImage.image'在这里 – nelsonK

+0

我没有得到任何错误,但它仍然无法正常工作。图像仍然平方。 – JoseMelendez

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 

    } 
+0

我对视角参数有什么要求? – JoseMelendez

+0

你可以试试这条线吗? - annotationView.image = getRoundedImage(originalImage:profileImage.image,view:annotationView,radius:self.annotationView.frame.width/2,borderWidth:1.0) - 替代annotationView.image = profileImage.image –

+0

它找到零当它解开这个上下文!.saveGState()在函数中。 – JoseMelendez

0

为了使广场转了一圈,只设置了角半径一半的宽度/高度。

imageview.layer.cornerRadius = 12.5 //Half the with/height of the imageview 
imageview.layer.masksToBounds = true