【问题标题】:How to create a circular image using MKAnnotationView如何使用 MKAnnotationView 创建圆形图像
【发布时间】:2014-09-21 09:17:34
【问题描述】:

我重写了 MKAnnotationView 以在地图上显示自定义图像:

override init(annotation: MKAnnotation!, reuseIdentifier: String!) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)

        let img = UIImage(named: "annotation")
        self.image = img
        self.frame = CGRectMake(0, 0, 40, 40)
        self.layer.cornerRadius = 20
        self.contentMode = UIViewContentMode.ScaleAspectFit
        self.centerOffset = CGPointMake(0, -20)
    }

我想在地图上创建一个圆形图像,但cornerRadius 不起作用。

【问题讨论】:

  • 试试self.layer.masksToBounds = true

标签: swift mapkit ios8 xcode6 mkannotationview


【解决方案1】:

您正在使用MKAnnotation,这是一个数据模型。您想查看 MKAnnotationView 和相关类以获取注释的可视部分。检查 MapKit 文档。

【讨论】:

  • 感谢您的回答。但我重写了 MKAnnotationView,事实上 self.image 被称为 MKAnnotationView。我需要从 UIImage(self.image)中检索 UIImageView,但我不知道该怎么做
【解决方案2】:

我已经解决了

   annotationView.layer.cornerRadius = 20
   annotationView.layer.masksToBounds = true

但是当我使用这个解决方案时,标注不会出现......

【讨论】:

    【解决方案3】:

    clipToBounds 将裁剪视图以匹配图层。

    annotationView.layer.cornerRadius = 20
    annotationView.clipsToBounds = true
    

    【讨论】:

    • 如果您提供解释为什么这是首选解决方案并解释它是如何工作的,它会更有帮助。我们想要教育,而不仅仅是提供代码。
    • 也许,但您的答案类似于 2014 年 OP 的答案。解释为什么您的答案比较合适对于正在寻找与 OP 相同问题的解决方案的其他人来说很重要.
    • 我的答案不相似......其他答案对我不起作用。我不是故意粗鲁的......我只是说是自我解释,因为代码设置了圆角半径,clipToBounds 将准确地剪辑视图以匹配图层
    猜你喜欢
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多