问题
目前我正在尝试将自定义图像添加到AnnotationPoint。我目前有它工作,但我想添加圆形边缘。当我尝试设置代码maskToBounds = true时,我会抛出一个错误(我相信因为它是在主线程中调用的)。当我尝试创建一个临时的AnnotationView时,在viewDidLoad中我将它的masksToBounds属性设置为true,它可以工作,但只适用于1人图像,当我放大地图时它会消失。我想要帮助的是弄清楚如何解决错误
我尝试过什么
//sets the pictures on the map from a url
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "Profile Picture"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
//Set annotation-specific properties **AFTER**
//the view is dequeued or created...
let cpa = annotation as! CustomPointAnnotation
if let url = NSURL(string: cpa.urlName) {
if let data = NSData(contentsOfURL: url){
anView.image = UIImage(data: data)
}
}
//resize the users friends profile pictures
var cropSquare = CGRectMake(0, 0, anView.image.size.width / 3, anView.image.size.height / 3)
UIGraphicsBeginImageContextWithOptions(cropSquare.size, false, 1.0)
anView.image.drawInRect(cropSquare)
anView.image = UIGraphicsGetImageFromCurrentImageContext()
anView.layer.masksToBounds = true
anView.layer.cornerRadius = 10
return anView
}
错误
线程1:信号SIGABRT
答案 0 :(得分:0)
//make the images circles
var imageView: UIImageView = UIImageView(image: anView.image)
var layer: CALayer = CALayer()
layer = imageView.layer
layer.masksToBounds = true
layer.cornerRadius = CGFloat(anView.image.size.width / 2)
UIGraphicsBeginImageContext(imageView.bounds.size)
layer.renderInContext(UIGraphicsGetCurrentContext())
var roundedImage = UIGraphicsGetImageFromCurrentImageContext()
anView.image = roundedImage
UIGraphicsEndImageContext()