ios - Swift MKAnnotationView maskToBounds 抛出错误

标签 ios swift

问题
目前我正在尝试将自定义图像添加到 AnnotationPoint。我目前正在使用它,但我想为其添加圆边。当我尝试设置代码 maskToBounds = true 时,我抛出了一个错误(我相信是因为它在主线程中被调用)。当我尝试制作一个临时的 AnnotationView 时,在 viewDidLoad 中我将它的 masksToBounds 属性设置为 true,并且它有效,但仅适用于 1 人图像,当我放大 map 时它会消失。我需要帮助的是弄清楚如何修复错误

我尝试过的

    //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

最佳答案

    //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()

关于ios - Swift MKAnnotationView maskToBounds 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32833839/

相关文章:

ios - iOS10如何清除缓存?

swift - 我似乎无法让外键在 SQLite.swift 中工作

ios - swift 2.0 : Best Practice for communication to a REST API with JSON

ios - UISegmentedControll 在选择段时更改高度

ios - 在另一个 View Controller 上设置动画的最简单方法?

ios - Parse PFSignUpViewController 的空电子邮件字段异常

ios - 来自 AVCaptureVideoPreviewLayer 的实时缩放

swift - UITableViewCell 滚动时继续更改其图像

swift - 如果空格或空白,如何不将 UITextField 中的项目追加到数组 - Swift

ios - UIAlertView alertViewShouldEnableFirstOtherButton : disables wrong button on iOS7