我正在使用Mapkit。在leftcalloutaccessory,有一个小图像,这是从用户位置采取的。在rightcalloutaccesory中,有一个按钮可以使另一个viewcontroller变为segue,因此用户可以看到大尺寸的图像。 问题是 - 它是随机的保存图像,它将显示在另一个视图上。发送挑选的图像到另一个视图
class ImageAnnotation: MKPointAnnotation {
var image: UIImage?
}
然后在地图视图控制器,它看起来像这样
var imageAnnotations: [ImageAnnotation] = []
var sendImage: UIImageView!
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotation = annotation as? ImageAnnotation else {
return nil
}
let identifier = "MyCustomAnnotation"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
let image = UIImage(named: "advance.png")
let button = UIButton(type: .DetailDisclosure)
button.frame = CGRectMake(0, 0, 30, 30)
button.setImage(image, forState: .Normal)
annotationView?.rightCalloutAccessoryView = button
let detailImage = UIImageView(frame: CGRectMake(0, 0, 50, 50))
detailImage.image = annotation.image
sendImage.image = annotation.image
annotationView?.leftCalloutAccessoryView = detailImage
return annotationView
}
它的工作原理。它在每个注释中都显示正确的图像。但“精美图像”,可以是每个保存的图像。
override func viewDidAppear(animated: Bool) {
super.viewWillAppear(animated)
for annotation in imageAnnotations {
mapView.addAnnotation(annotation)
}
gettingData()
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
{
if (view.annotation is MKPointAnnotation) {
print("Clicked annotation ")
if control == view.rightCalloutAccessoryView {
sendImage.image = UIImage(named: "advance.png")
}
}
}
func buttonPressed (sender: UIButton!) {
let currentImage: UIImage = sender.imageForState(.Normal)!
sendImage.image = currentImage
self.performSegueWithIdentifier("ShowLargeImage", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowLargeImage" {
let goToImageViewController = segue.destinationViewController as! ImageViewController
goToImageViewController.newImage = sendImage.image
}
}
在'let currentImage:UIImage = sender.imageForState(.Normal)!中删除这两行! sendImage.image = currentImage'在这里'func buttonPressed(sender:UIButton!)' –
仍然说'意外地发现零,同时展开可选值'。找不到它发生的位置 – jonask
确定在哪一行看到错误 –