Passing Image to Another View Controller (Swift)

pass qr image to another view controller

what are you doing:

let image: UIImage = UIImage() // <- its create image
let imageView: UIImageView = UIImageView() // its create some kind of container

imageView = image // ERROR!

you trying to set object with type UIImage to object with type UIImageView

correct:

let image: UIImage = UIImage() 
let imageView: UIImageView = UIImageView()

imageView.image = image

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 an image from one viewcontroller to another viewcontroller in swift?

You're getting a crash here:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let productdetails = storyboard.instantiateViewController(withIdentifier: "LocatemypicViewController") as! LocatemypicViewController
print(imgobj.image)
productdetails.qrimgobj.image = imgobj.image

Specifically in this line: productdetails.qrimgobj.image = imgobj.image... This is because you are trying to access or rather passing an object (UIImage) to your qrimgobj which I presume is a UIImageView while that qrimgobj property is not yet initialized. One of the things you can do is to have a property that holds the image temporarily and then pass it to qrimgobj after the whole screen is loaded.

Example:

var qrimage: UIImage?

viewDidLoad() {
super.viewDidLoad()
self.qrimgobj.image = self.qrimage // Pass
}

Pass image from one view controller to another controller gives nil

You should use segue.destination not create a new one , like this

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

print("In here")

if segue.identifier == "showImage" {

let next = segue.destination as! showImageController

next.newImage = UIImage(named: imageArray[0])

}

}


Related Topics



Leave a reply



Submit