【问题标题】:assign image to annotation instead of pin将图像分配给注释而不是引脚
【发布时间】:2016-11-30 09:45:52
【问题描述】:

大家好,我正在尝试将注释上的图钉替换为自定义图像。

我的 image.cassette 列表中已经有图像,将显示 1X 2X 3X 等,但是当我尝试调用该图像时,我的代码中出现错误,Xcode 尝试修复我的语法但最终抛出它是错误的

下面列出的我的代码覆盖函数

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    guard !(annotation is MKUserLocation) else {
        return nil
    }


    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView =   mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }

    if let annotationView = annotationView {

        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "House")
    }

    return annotationView
}

现在Xcode 在代码 annotationView.image = UIImage(named: "House") 中发现错误,它要求插入 ",",最终结果如下所示

annotationView.image = UIImage(named: "#imageLiteral(resourceName: ",House,")")

Xcode 将显示错误使用未解析的标识符“House”

【问题讨论】:

  • 错误说明了什么?
  • 使用未解析的标识符“House”

标签: ios swift3 mkpinannotationview


【解决方案1】:

Swift 3 引入了图像文字。只需写“Imag”.. 它会打开带有 Image Literal 选项的弹出窗口。如下图:

通过选择“图像文字”选项,它将弹出所有可用的资产图像,

从网格中选择要使用的图像。

更多详情,see this

【讨论】:

    【解决方案2】:

    这段代码帮助我解决了我的问题

    let pinImage = UIImage(named: "House.png")
    annotationView.image = pinImage
    

    但我现在遇到的问题是我的地图不再居中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多