How to save a photo to the camera roll Xcode?

In my project, the user should be able to take a photo, and save it to the camera roll. How do I add this functionality to my code. I have already added the save photo button, and maybe I call a function to save it. Someone please assist me in doing this thanks. My code down below:

//

//  ViewController.swift

//  takePictureWithCamera

//

//  Created by Vaibhav Satishkumar on 11/20/21.

//



import UIKit



class ViewController: UIViewController {

    

    @IBAction func savePhoto(_ sender: Any) {

         

    }

    

    @IBOutlet var imageView: UIImageView!

    @IBOutlet var button: UIButton!

        

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        imageView.backgroundColor = .secondarySystemBackground

        

        

        

        

    }

    

    @IBAction func didTapButton(){

        let picker = UIImagePickerController()

        picker.sourceType = .camera

        picker.allowsEditing = true

        picker.delegate = self

        present(picker, animated: true)

        

        

    }

    

    





}





extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{

    

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

        picker.dismiss(animated: true, completion: nil)

        

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        picker.dismiss(animated: true, completion: nil)

        guard let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else

        {

            return

        }

        

        imageView.image = image

        

        

    }

}

You will find complete solution here:

https://stackoverflow.com/questions/40854886/take-a-photo-and-save-to-photo-library-in-swift

Don't forget to add authorisation request in plist.

Note: when you paste code, lease use "Paste and Match Style" to avoid all the blank lines that make code nearly unreadable.

How to save a photo to the camera roll Xcode?
 
 
Q