2016-08-03 59 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

相关问题