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.
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.
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)")
}
}
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With