【发布时间】:2021-02-01 15:34:23
【问题描述】:
我有一个应用程序,用户按下按钮并拍照,然后照片进入 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
}
}
【问题讨论】:
-
你明白错误信息是什么意思吗?
标签: ios mapkit mkmapview swift5 mapkitannotation