2017-06-03 22 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