2016-08-03 13 views
0

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 
    } 
} 
+0

diese beiden Zeilen in 'lassen currentImage entfernen: UIImage = sender.imageForState (.Normal)! sendImage.image = currentImage' hier 'func button (Sender: UIButton)' –

+0

noch sagen: 'unerwartet null gefunden, während ein optionaler Wert auspackt'. Kann nicht herausfinden, wo es passiert – jonask

+0

ok in welcher Zeile Sie den Fehler –

Antwort

1

Im Ziel-View-Controller:

class ImageViewController: UIViewController 
{ 
    @IBOutlet weak var finalImage: UIImageView! // assume that its your imageViewname 

    var newImage: UIImage! // assume that it is pass Image 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     if let img = newImage 
     { 
     finalImage.image = img 
     } 
    } 
} 

Im Quelle-View-Controller:

//create one Common ImageView 
var sendImage: UIImageView! 

func buttonPressed (sender: UIButton!) { 


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 // directly pass the image no need of converson 
} 
} 

zugleich Zahl hier auch

let detailImage = UIImageView(frame: CGRectMake(0, 0, 50, 50)) 
detailImage.image = annotation.image 
sendImage.image = annotation.image 
annotationView?.leftCalloutAccessoryView = detailImage 

aktualisiert Antwort

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") 
} 
} 

} 
+0

Es funktioniert! Ich musste das detailImage außerhalb der Funktion machen. Vielen Dank, hat mich schon lange belästigt! – jonask

+0

@jonask - die Wahl liegt bei Ihnen, machen Sie gut –

+0

Oh warte. Es wird ein neues Problem geben. Jetzt ist es zufällig, welches Bild im linken CallAccessory sein wird. – jonask

Verwandte Themen