jonask jonask - 4 months ago 10
iOS Question

Sending picked image to another view

Im working with Mapkit. In the leftcalloutaccessory, there is a small image, thats been taken from the userlocation. In the rightcalloutaccesory, there is a button, that will make a segue, to another viewcontroller, so the user can see the image in large size.
The issue is - it is random which saved image, that will be shown on the another view.

class ImageAnnotation: MKPointAnnotation {
var image: UIImage?
}


Then in the Map view controller it looks like this

var pickedimage : NSData!
var imageAnnotations: [ImageAnnotation] = []

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
annotationView?.leftCalloutAccessoryView = detailImage

pickedimage = UIImagePNGRepresentation(annotation.image!)

return annotationView
}


It works. Its showing the right image in every annotation. But "pickedimage", can be every saved images.

override func viewDidAppear(animated: Bool) {
super.viewWillAppear(animated)

for annotation in imageAnnotations {
mapView.addAnnotation(annotation)
}

gettingData()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowLargeImage" {
let goToImageViewController = segue.destinationViewController as! ImageViewController

goToImageViewController.fishDataImage = pickedimage
}
}

func buttonPressed (sender: UIButton!) {

self.performSegueWithIdentifier("ShowLargeImage", sender: self)
}

Answer

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

In Source view controller:

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 = detailImage.image // or use annotation.image! // directly pass the image no need of converson
}
}