Passing an image from one view controller to its previous view controller

I have created an image view and I'm putting some image into it by either choosing from the photo library or camera.

The image gets set on the UIImageView but as soon as i go back to the previous view controller (say firstVC) and come back the photo disappears.

Also, on the previous view controller (firstVC), I want the image that is set to be reflected in this view controller.

I'm using a navigation controller to go back and forth so no segue identifier and all is present.

Can anybody help me with code for keeping the profile picture even when I navigate back and forth and also the picture should be reflected in the firstVC.

5 answers

  • answered 2018-09-17 06:01 Glenn

    There are lots of ways to have what you want. The easiest way is to have a global class that manages or handles your data for global access, like so:

    import UIKit
    
    struct SharedDataManager {
        static var chosenImage: UIImage?
    }
    

    Now you can both set and get some UIImage data into that object, like:

    SET: SharedDataManager.chosenImage = UIImage(named: "myImage")!

    GET:

    guard let storedImage = SharedDataManager.chosenImage else { return }
    UIImageView(image: storedImage)
    

    Hope this helps!

  • answered 2018-09-17 06:01 Tobi

    i really recommends reading this article about Delegates

    in iOS there is too many ways to pass data between viewControllers, in your case Delegates is the one. its pretty common too, Delegates are protocols that a VC can confirm to, fired up in a different VC that simple although it can be very misleading follow this article and you are good to go

  • answered 2018-09-17 06:08 midhun p

    please use unwind segue, it's the best approach for sending data to it's predecessor view controller

  • answered 2018-09-17 06:28 Ankit Jayaswal

    You can use closures to send image from SecondVC to firstVC.

    1. You need to define closure to SecondVC as:

        var returnClosure: ((UIImage) -> Void)?
    

    Call this closure when you need to send this image to previous view controller, as:

        func sendImageToPreviousVC(image: UIImage) {
            self.returnClosure?(image)
        }
    

    2. You have to handle this closure in FirstVC, as:

        @IBAction func actionPushToSecondVC(_ sender: UIButton) {
            if let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondVC") as? SecondViewController {
                vc.returnClosure = { image in
                    // Store image here and use it as per your need.
                }
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    

  • answered 2018-09-17 06:39 Naresh

    We have two view controllers, one DataSender Delegate

    First View Controller Duty: Will receive the image from SecondViewController

    Second View Controller. Duty: select image from camera or gallery & send to FistViewController

    DataSenderDelegate: Duty: make communication between both view controllers

    //This protocol willmake communication between the view Controllers.
    
        protocol DataSenderDelegate: class {
    
            func imageRecieved(image: UIImage)
        }
    
    /**
     This class is the first view controller.
     You will recieve the image in this view controller from second view controller.
     */
    class FirstViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        //Navigate to second view controller
        /**
         Note: call this method to navigate to SecondViewController.
         Don't forget to assign the response delegate.
         */
        func navigateToSecondViewController() {
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "\(SecondViewController.classForCoder())") as! SecondViewController
            self.navigationController?.pushViewController(vc, animated: true)
            //This will assign the response delegate of SecondVC to self (This View Controller).
            vc.bind(withResponseDelegate: self)
        }
    
    }
    
    extension FirstViewController: DataSenderDelegate {
    
        //This delegate functions will be called from second view controller.
        func imageRecieved(image: UIImage) {
            //TODO: show image from here
        }
    }
    
    
    /**
     This class is the second view controller.
     You will selecte image from camera or galler from this view controller.
     */
    class SecondViewController: UIViewController {
    
        //Delegate instance varibale which will call the imageRecieved(image:) function
        fileprivate weak var delegate: DataSenderDelegate?
    
        @IBOutlet var profileImageView: UIImageView! {
            didSet {
                //When image will be set the the preceeding View controller's imageRecieved function will be called.
                guard let image = profileImageView.image else { return  }
                self.delegate?.imageRecieved(image: image)
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        //MARK:-
        //This function assign the delegate
        func bind(withResponseDelegate responseDelegate: DataSenderDelegate) {
            self.delegate = responseDelegate
        }
    
    
    }