将拾取的图像发送到另一个视图

时间:2016-08-03 11:57:06

标签: ios swift uiviewcontroller uiimage mapkit

我正在使用Mapkit。在leftcalloutaccessory中,有一个小图像,它是从用户位置获取的。在rightcalloutaccesory中,有一个按钮,它将成为另一个视图控制器的segue,因此用户可以看到大尺寸的图像。 问题是 - 它是随机的保存图像,将显示在另一个视图上。

class ImageAnnotation: MKPointAnnotation  {
var image: UIImage?
}

然后在Map视图控制器中,它看起来像这个

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
}

有效。它在每个注释中显示正确的图像。但是“pickimage”,可以是每个保存的图像。

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

1 个答案:

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

}