Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take picture automatically by programming in Swift?

I am developing in Swift with iPhone camera.

I using the following code to check the camera:

if UIImagePickerController.isSourceTypeAvailable(.Camera) {   
    let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = UIImagePickerControllerSourceType.Camera
        picker.allowsEditing = true  
        self.presentViewController(picker, animated: true)
    } else {
        print("can't find camera")
    }
}

And I use the following code to open the camera.

presentViewController(imagePicker, animated: true, completion: nil)

I have reference the following link and call takePicture() , but it seems did not working.

But I always need to manually take the picture by tapping the shutter button. Is programmatically taking the picture possible in Swift?

Thanks in advance.

like image 800
Martin Avatar asked Apr 26 '16 10:04

Martin


People also ask

What is image picker in Swift?

Overview. An image picker controller manages user interactions and delivers the results of those interactions to a delegate object. The role and appearance of an image picker controller depend on the source type you assign to it before you present it.


2 Answers

1) Check if you have these delegates in your view controller class:

 UIImagePickerControllerDelegate, UINavigationControllerDelegate

2) Check if your plist have the permissions with YES

Privacy - Photo Library Usage Description
Privacy - Camera Usage Description

If you want, I use this code (swift 3):

    let imagePicker = UIImagePickerController()
    imagePicker.sourceType = .photoLibrary
    imagePicker.delegate =  self
    self.present(imagePicker, animated: true, completion: nil)

For camera, just change "photoLibrary"

    imagePicker.sourceType = .camera

3) In order to get the picture, you must implement the delegate didFinishPickingMediaWithInfo:

extension YourViewController: 
                             UIImagePickerControllerDelegate, 
                             UINavigationControllerDelegate 
{

  func imagePickerController(
              _ picker:                           UIImagePickerController,  
              didFinishPickingMediaWithInfo info: [String : Any]) 
  {
    imagePicker.dismiss(animated: true, completion: nil)
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    print("picture taken: \(image)")
  }
}
like image 189
Renata Faria Avatar answered Nov 14 '22 23:11

Renata Faria


    var imagePicker:UIImagePickerController = UIImagePickerController()        
    //Set delegate to imagePicker
    imagePicker.delegate = self
    //Allow user crop or not after take picture
    imagePicker.allowsEditing = false
    //set what you need to use "camera or photo library"
    imagePicker.sourceType = .camera
    //Switch flash camera
    imagePicker.cameraFlashMode = .off
    //Set camera Capture Mode photo or Video
    imagePicker.cameraCaptureMode = .photo
    //set camera front or rear
    imagePicker.cameraDevice = .rear
    //Present camera viewcontroller
    self.present(imagePicker, animated: true, completion: nil) 

and don't forget implement two protocol: UIImagePickerControllerDelegate,UINavigationControllerDelegate

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let imagePicked                              = info[UIImagePickerControllerOriginalImage] as? UIImage {
        UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil)
        dismiss(animated: true, completion: nil)
    }
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}
like image 25
Sour LeangChhean Avatar answered Nov 14 '22 22:11

Sour LeangChhean