如何在 MKMapView 中显示 UIImage

How to show UIImage in MKMapView

我的视图中有一个 MKMapView,如果找不到特定位置,我想在其中显示一个 UIImage。 swift 4 有什么办法吗?

您可以在 MKMapView 中添加图像作为注释,借助于使用图像视图创建自定义视图,并在运行时设置图像,并借助以下代码从视图中获取图像。

func imageWithView(view : UIView) -> UIImage
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)

    view.layer.render(in: UIGraphicsGetCurrentContext()!)

    let myimg : UIImage = UIGraphicsGetImageFromCurrentImageContext()!

    UIGraphicsEndImageContext()
    return myimg
}

试试下面的代码:

    let imageView = UIImageView(frame: mapView.frame)
    imageView.image = UIImage(named: "YOUR IMAGE NAME")
    mapView.addSubview(imageView)

当我将框架设置为绑定时,添加到 Kosuke Ogawa 的回答中,它解决了我的问题。

let imageView = UIImageView(frame: mapView.bound)
imageView.image = UIImage(named: "YOUR IMAGE NAME")
mapView.addSubview(imageView)