gpt4 book ai didi

ios - Swift MKAnnotationView maskToBounds 抛出错误

转载 作者:搜寻专家 更新时间:2023-11-01 05:39:08 25 4
gpt4 key购买 nike

问题
目前我正在尝试将自定义图像添加到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com