Ios 地图上的自定义注释

Ios 地图上的自定义注释,ios,mapkit,mkmapview,swift5,mapkitannotation,Ios,Mapkit,Mkmapview,Swift5,Mapkitannotation,我有一个应用程序,用户按下一个按钮并拍摄一张照片,然后照片转到ImageAnnotation类,必须将其添加到地图中,但我得到这样一个错误:“在隐式展开可选对象时意外发现” 只有这个错误才能阻止应用程序正常工作,如果你能帮助我,我将不胜感激 这是代码 extension MapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { func imagePickerControllerDi

我有一个应用程序,用户按下一个按钮并拍摄一张照片,然后照片转到ImageAnnotation类,必须将其添加到地图中,但我得到这样一个错误:“在隐式展开可选对象时意外发现”

只有这个错误才能阻止应用程序正常工作,如果你能帮助我,我将不胜感激

这是代码

extension MapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    picker.dismiss(animated: true, completion: nil)
    
    guard let ecoImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
    
    guard let currentLocation = locationManager.location else { return }

    let pin = ImageAnnotation(coordinate: CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude), image: ecoImage, color: UIColor.systemGreen)
}
}

下面是必须为地图创建自定义注释的类

class ImageAnnotation : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var imageEco: UIImage?
var color: UIColor?
var imageView: UIImageView!

init(coordinate: CLLocationCoordinate2D, image: UIImage, color: UIColor) {
    self.coordinate = coordinate
    imageEco = image
    self.color = color
    imageView.image = image
    imageView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    self.imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    imageView.addSubview(self.imageView)

    self.imageView.layer.cornerRadius = 25
    self.imageView.layer.masksToBounds = true
}
}


你说的是
imageView.image=image
。但是
imageView
nil
。所以你崩溃了。没什么大不了的


从更大的角度来看,您似乎没有理解注释和注释视图之间的区别。注释只是一个信息载体:它可以说明坐标是什么,或者应该显示什么图像,但仅此而已。地图视图代表的工作是按需创建相应的注释视图,这是您处理视图的地方,可以安排视图显示圆形图像。

您理解错误消息的含义吗?