Ich arbeite mit Mapkit. Im linken CallAccessory befindet sich ein kleines Bild, das aus dem Userlocation entnommen wurde. Im rechtenCalloutaccesory gibt es eine Schaltfläche, die einen Übergang zu einem anderen View-Controller macht, so dass der Benutzer das Bild in großer Größe sehen kann. Das Problem ist - es ist zufällig das gespeicherte Bild, das in der anderen Ansicht angezeigt wird.Gescanntes Bild zu einer anderen Ansicht senden
class ImageAnnotation: MKPointAnnotation {
var image: UIImage?
}
Dann in der Map-View-Controller sieht es aus wie dieses
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
}
Es funktioniert. Es zeigt das richtige Bild in jeder Anmerkung. Aber "pickeimage", kann jeder gespeicherte Bilder sein.
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
}
}
diese beiden Zeilen in 'lassen currentImage entfernen: UIImage = sender.imageForState (.Normal)! sendImage.image = currentImage' hier 'func button (Sender: UIButton)' –
noch sagen: 'unerwartet null gefunden, während ein optionaler Wert auspackt'. Kann nicht herausfinden, wo es passiert – jonask
ok in welcher Zeile Sie den Fehler –