How to Segue an Image to Another Viewcontroller and Display It Within an Imageview

How do I segue an image to another ViewController and display it within an ImageView?

Edit 2

Given that you have too many mistakes in your code, I'm going to a bit more specific.

First: You need to understand the difference between UIImage and UIImageView

Second: If you want to perform segue after the user selects an image, you should call the method on the didFinishPickingMediaWithInfo delegate, like this:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
image = imageView.image!
performSegueWithIdentifier("mySegueToNextViewController", sender: self)
}

Also, I added a new property called "image" (should be called "selectedImage", but you can change it later).

private var image: UIImage? // THIS ONE

@IBOutlet weak var btnClickMe: UIButton!
@IBOutlet weak var imageView: UIImageView!

Third: On the ViewController2 you need to set the image library to the UIImageView.

override func viewDidLoad() {
super.viewDidLoad()
imageView.image = selectedImage
}

Finally: In the Storyboard, select ViewController, go to editor -> Embed In -> Navigation Controller.

Now, should work just fine.



Once you select the image, call the following method:

performSegueWithIdentifier("mySegueToNextViewController", sender: self)

Then you need to implement:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegueToNextViewController" {
if let nextViewController = segue.destinationViewController as? NextViewController {

nextViewController.image = selectedImage
}
}

}

Keep in mind that for this to work, you should have public property on NextViewController (just like Unis Barakat said).
Then on the NextViewController viewDidLoad() you could set the image to the imageView.

Hope this helps!

How to pass images through segue into new view controller?

Problem is in this line detailCrystalPhotoImageView.image = UIImage(named: img). Here argument named is string type. Here you are passing img which UIImage type. So you can fix this by

detailCrystalPhotoImageView.image = img

How to transfer a photo from one viewcontroller to a UIImageView of another viewcontroller

Destination controller's view is not loaded in prepare(for:), It's not the best practice of passing data between controllers but you can make sure that view is loaded before touching any if it's subviews using outlets:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)  {

if segue.identifier == "segue" {

if let detail = segue.destination as? DetailViewController {
detail.loadViewIfNeeded()
detail.PhotoProfile = selectedImage
}
}
}

how to pass a UI ImageView from one view controller to another? swift 3

You didn't show PhotoShareLabelViewController, but presumably it has:

var thisString: String?

Add

var thisImage: UIImage?

And then pass it along the same way as thisString

photoShareLabelViewController.thisImage = imageView.image

And pick it up in the viewDidLoad of PhotoShareLabelViewController (which is probably where you pick up thisString). Something like:

self.imageView.image = self.thisImage

But, using whatever variable name you are using.

In short, just copy what you are doing for thisString, but use a UIImage as the type instead of String.

NOTE: You are not passing a UIImageView from one VC to another (that is impossible). You are passing the image from an imageview from one VC to another.

Segue Image to next ViewController (Swift)

Just send

ImageStore.image

It should solve the issue. If you want to send image instead of imageView.

How can I make a UIImage View clickable to segue to another view controller

The easiest thing to do is to use a UIButton instead of an image view. Set it's type to custom and install the image into the button in IB (Interface Builder).

That way you can trigger an IBAction just like any other button. It also highlights on a touch like you'd expect, triggers on touch up rather than touch down, etc.

If you don't want to use a button-with-image, you have to set userInteractionEnabled = YES on the image view and install a tap gesture recognizer on it. See the docs on UITapGestureRecognizer for more information.



Related Topics



Leave a reply



Submit