2016-08-03 53 views
0

我正在使用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 
    } 
} 
+0

在'let currentImage:UIImage = sender.imageForState(.Normal)!中刪除這兩行! sendImage.image = currentImage'在這裏'func buttonPressed(sender:UIButton!)' –

+0

仍然說'意外地發現零,同時展開可選值'。找不到它發生的位置 – jonask

+0

確定在哪一行看到錯誤 –

回答

1

在目的地視圖控制器:

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

在源視圖控制器:

//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 
} 
} 

在同一時間在這裏補充也

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

更新答案

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

它的工作原理!我不得不在功能之外製作detailImage。非常感謝,困擾了我很長一段時間! – jonask

+0

@jonask - 選擇是你的,做得好 –

+0

哦,等等。會有一個新問題。現在,它是隨機的,將在左邊休息區的圖像。 – jonask

相關問題