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