Swift 3 /4 take photo from camera or gallery and show it .

Home  >>  Tutorials  >>  IOS  >>  Swift 3 /4 take photo from camera or gallery and show it .

Swift 3 /4 take photo from camera or gallery and show it .

1
Nov,2017

0

Step One : First create a project and take imageView and a button to capture image . Connect it to ViewController .

Step Two : Copy this code and run . hope it will be done . ūüėÄ



   class ImageCapture: UIViewController , UIImagePickerControllerDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate{



     @IBOutlet var imageViewWeight: UIImageView!

     var imagePicker:UIImagePickerController?=UIImagePickerController()

override func viewDidLoad() {

        super.viewDidLoad()

         imagePicker?.delegate=self

    }

    



@IBAction func takeImage(_ sender: Any) {

      var alert = UIAlertController(title: "Choose type" , message: "Select image" , preferredStyle: .actionSheet);

      var photos = UIAlertAction(title : "Camera" , style: .default , handler: { (alert: UIAlertAction ) in

          self.openCamera()

    })

        var videos = UIAlertAction(title : "Gallery" , style: .default , handler: { (alert: UIAlertAction ) in

           self.openGallery()

      })

        

        var cancel = UIAlertAction(title : "Calcel" , style: .default , handler: nil)

        alert.addAction(photos)

        alert.addAction(videos)

        alert.addAction(cancel)

        present(alert, animated: true , completion: nil)

    }

 func openCamera () {

        

        if UIImagePickerController.isSourceTypeAvailable(.camera){

            imagePicker?.delegate = self

            imagePicker?.sourceType = .camera;

            imagePicker?.allowsEditing = true

            imagePicker?.mediaTypes = ["public.image"]

            

            self.present(imagePicker!, animated: true, completion: nil)

            

        }else{

            print("Camera not awailable")

        }

   }

    

    func openGallery() {

        

        if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){

            imagePicker?.delegate = self

            imagePicker?.sourceType = .savedPhotosAlbum;

            imagePicker?.allowsEditing = true

            imagePicker?.mediaTypes = ["public.image","public.movie"]

            self.present(imagePicker!, animated: true, completion: nil)

        }

      }

   func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        self.view.endEditing(true)

        return false

    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {

        self.dismiss(animated: true, completion: { () -> Void in

            

        })

        

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

    {

       imageViewWeight.image = info[UIImagePickerControllerOriginalImage] as? UIImage

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

Leave a Reply

Your email address will not be published. Required fields are marked *