Take Photos Using UIImagePickerController Swift 5

The UIImagePickerController class used for taking Photos and movies from Photo Library or Camera. You can choose image or Movie which is saved in PhotoLibrary or Camera. 




The UIImagePickerController manages the user interface and giving the result of the user in its delegate method. This tutorials for both Movie and Photos. You have to select sourceType from where you want to Photos or Movie.

If you select SourceType  UIImagePickerControllerSourceTypeCamera provide user for new Photos or Movie from supported device. and select  SourceType UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeSavedPhotosAlbum provide to choose photos or movie from Saved.


Step 1 : Create a variable for UIImagePickerController and UIImageView.
var imagPickUp : UIImagePickerController!
var imageV : UIImageView!
Step 2 : Adding delegate method of UIImagePickerController in ViewController interface.
class ViewController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate {
Step 3 : Create one function for init UIImagePickerController which return its object of imagePicker. 
func imageAndVideos()-> UIImagePickerController{
    if(imagPickUp == nil){
        imagPickUp = UIImagePickerController()
        imagPickUp.delegate = self
        imagPickUp.allowsEditing = false
    }
    return imagPickUp
}

Step 4 : ViewDidLoad in this method, Create a ImageView, One UIButton and also create imagePickerObject.
override func viewDidLoad() {
    super.viewDidLoad()

    imagPickUp = self.imageAndVideos()

    let button = UIButton(frame: CGRect(x: 100, y: 400, width: 200, height: 50))
    button.center = view.center
    button.backgroundColor = .black
    button.setTitle("Choose Image", for: .normal)
    button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
    self.view.addSubview(button)


    imageV = UIImageView(frame: CGRect(x: 100, y: 200, width: 200, height: 150))
    imageV.layer.cornerRadius = 10
    imageV.clipsToBounds = true
    imageV.layer.borderWidth = 2.0
    imageV.layer.borderColor = UIColor.red.cgColor
    view.addSubview(imageV)

}

Step 5 : OnClick button event, Open Action sheet to choose image from where you want to take a image Library or Camera,  
@objc func buttonClicked() {
    let ActionSheet = UIAlertController(title: nil, message: "Select Photo", preferredStyle: .actionSheet)

    let cameraPhoto = UIAlertAction(title: "Camera", style: .default, handler: {
        (alert: UIAlertAction) -> Void in
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){

            imagPickUp.mediaTypes = ["public.image"]
            imagPickUp.sourceType = UIImagePickerController.SourceType.camera;
            self.present(imagPickUp, animated: true, completion: nil)
        }
        else{
            UIAlertController(title: "iOSDevCenter", message: "No Camera available.", preferredStyle: .alert).show(self, sender: nil);
        }

    })

    let PhotoLibrary = UIAlertAction(title: "Photo Library", style: .default, handler: {
        (alert: UIAlertAction) -> Void in
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagPickUp.mediaTypes = ["public.image"]
            imagPickUp.sourceType = UIImagePickerController.SourceType.photoLibrary;
            self.present(imagPickUp, animated: true, completion: nil)
        }

    })

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
        (alert: UIAlertAction) -> Void in

    })

    ActionSheet.addAction(cameraPhoto)
    ActionSheet.addAction(PhotoLibrary)
    ActionSheet.addAction(cancelAction)


    if UIDevice.current.userInterfaceIdiom == .pad{
        let presentC : UIPopoverPresentationController  = ActionSheet.popoverPresentationController!
        presentC.sourceView = self.view
        presentC.sourceRect = self.view.bounds
        self.present(ActionSheet, animated: true, completion: nil)
    }
    else{
        self.present(ActionSheet, animated: true, completion: nil)
    }
}
Step 6 : UIImagePickerController Delegate method where your image result and take image from delegate use the image in your apps.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
    imageV.image = image
    imagPickUp.dismiss(animated: true, completion: { () -> Void in
        // Dismiss
    })

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    imagPickUp.dismiss(animated: true, completion: { () -> Void in
        // Dismiss
    })
}
Output:




Thanks 

Take Photos Using UIImagePickerController Swift 5 Take Photos Using UIImagePickerController Swift 5 Reviewed by KIRIT MODI on 00:30:00 Rating: 5

1 comment:

Powered by Blogger.