本文介绍了如何在Swift中创建带有边框和阴影的圆角图像作为MKAnnotationView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建看起来像下面的MKAnnotationView.
I'd like to create MKAnnotationView that looks like the one below.
我已经拥有的代码:
func maskRoundedImage(image: UIImage, radius: Float) -> UIImage {
let imageView: UIImageView = UIImageView(image: image)
var layer: CALayer = CALayer()
layer = imageView.layer
layer.masksToBounds = true
layer.cornerRadius = CGFloat(radius)
layer.borderWidth = 4.0
layer.borderColor = UIColor.whiteColor().CGColor
UIGraphicsBeginImageContext(imageView.bounds.size)
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundedImage
}
func imageResize (image:UIImage, sizeChange:CGSize)-> UIImage{
let hasAlpha = true
let scale: CGFloat = 0.0 // Use scale factor of main screen
UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
image.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
return scaledImage
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let annotationReuseId = "Place"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationReuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationReuseId)
} else {
anView!.annotation = annotation
}
let imageString = User.UserData.base64String
let image = convertBase64ToImage(imageString!)
let size = CGSizeMake(40, 40)
let resizedImage = imageResize(image, sizeChange: size)
anView?.image = maskRoundedImage(resizedImage, radius: 20)
anView!.backgroundColor = UIColor.clearColor()
anView!.canShowCallout = true
return anView
}
问题是我的图像质量很差.您有什么想法可以提高质量以及如何添加阴影吗?
The thing is my image is very poor quality. Do you have any ideas what I can do to improve the quality and how to add the shadow?
推荐答案
在返回新的"anView"之前,您可以使用以下代码:
before returning your new "anView" you can use this code:
anView!.layer.cornerRadius = anView!.frame.size.height/2
anView!.layer.masksToBounds = true
anView!.layer.borderColor = UIColor.whiteColor()
anView!.layer.borderWidth = 5
这将使其带有白色边框.
That'll make it round with a white border.
如果您想在图像本身上加边框,则可以使用图像视图"并将其作为MKAnnotationView的子视图.
If instead you want a border on the image itself you could just use an Image View and put it as a subview of your MKAnnotationView.
这篇关于如何在Swift中创建带有边框和阴影的圆角图像作为MKAnnotationView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!