how to autocapture an image from camera by using UIImagePickerController in swift - swift

I am trying to capture a picture automatically from camera without clicking the camera capture-button and i am using the UIImagePickerController for camera usage.what should I have to add for autocapture?
class ViewController:UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate
{
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
self.openCamera()
}
func openCamera() {
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)) {
imagePicker.sourceType = UIImagePickerController.SourceType.camera
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
self.imagePicker.dismiss(animated: true, completion: nil)
guard let selectedImage = info[.originalImage] as? UIImage
else{
print("Image not found!")
return
}
}
Here I am clicking the capture button. But I want to take automatically a picture after autofocusing.

It is possible using UIImagePickerController as I R&D on this functionality.
If you want to autocapture an image from camera then you can use this below mentioned code.
1. First you need to Add AVFoundation framework in your project.
Select project -> Add AVFoundation framework in Frameworks, Libraries, and Embedded Content Section
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate {
let objCaptureSession = AVCaptureSession()
var objPreviewLayer : AVCaptureVideoPreviewLayer?
var objCaptureDevice : AVCaptureDevice?
var objImagePicker = UIImagePickerController()
var objTimer: Timer?
var flagRepeatRecording: Bool = false
var objOutputVolumeObserve: NSKeyValueObservation?
let objAudioSession = AVAudioSession.sharedInstance()
override func viewDidAppear(_ animated: Bool) {
if !flagRepeatRecording {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
objImagePicker = UIImagePickerController()
objImagePicker.delegate = self
objImagePicker.sourceType = .camera
objImagePicker.allowsEditing = false
objImagePicker.showsCameraControls = false
//imagePicker.mediaTypes = [kUTTypeMovie as String] // If you want to start auto recording video by camera
} else {
debugPrint("Simulator has no camera")
}
self.present(self.objImagePicker, animated: true, completion: {
self.objImagePicker.takePicture() // If you want you auto start video capturing then replace this code with take picture() method startVideoCapture()
NotificationCenter.default.addObserver(self, selector: #selector(self.cameraIsReady), name: .AVCaptureSessionDidStartRunning, object: nil)
flagRepeatRecording = true
}
}
#objc func cameraIsReady(notification: NSNotification) {
DispatchQueue.main.async {
self.objImagePicker.takePicture()
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// let tempImage = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL?
// let pathString = tempImage?.relativePath
self.dismiss(animated: true, completion: {})
// UISaveVideoAtPathToSavedPhotosAlbum(pathString!, self, nil, nil)
self.objTimer?.invalidate()
self.objTimer = nil
}
}

Related

Select Video Instead of Image using ImagePickerController - Swift

My situation is that I have a working imagepickercontroller that allows the user to pick an image from their camera roll and display it on an imageview inside the application.
The problem is that I also want to be able to do the same thing with videos, and instead, display the video on an avplayer. I've done some research but couldn't find any good sources.
Can someone show me how to do this? possibly by editing the code below?
Thanks in advance!
This is the code I used for importing and displaying images from cameraroll (all above the viewDidLoad()):
#IBOutlet weak var imageView: UIImageView!
// the image picker controller
var imagePicker = UIImagePickerController()
// this is the button you tap to import your photo
#IBAction func imageViewButtonTapped(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
imagePicker.delegate = self
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info[.editedImage] as? UIImage{
selectedImageFromPicker = editedImage
}else if let originalImage = info[.originalImage] as? UIImage{
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
imageView.image = selectedImage
}
dismiss(animated: true, completion: nil)
}
try this:
import AVFoundation
class VideoHelper {
static func startMediaBrowser(delegate: UIViewController & UINavigationControllerDelegate & UIImagePickerControllerDelegate, sourceType: UIImagePickerController.SourceType) {
guard UIImagePickerController.isSourceTypeAvailable(sourceType) else { return }
let mediaUI = UIImagePickerController()
mediaUI.sourceType = sourceType
mediaUI.mediaTypes = [kUTTypeMovie as String]
mediaUI.allowsEditing = true
mediaUI.delegate = delegate
delegate.present(mediaUI, animated: true, completion: nil)
}
}
and you can use this as:
let source = UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) ? UIImagePickerController.SourceType.camera : UIImagePickerController.SourceType.savedPhotosAlbum
VideoHelper.startMediaBrowser(delegate: self, sourceType: source)

delegate method doesn't get called for UIimagePickerController

I am trying to create protocol where I can open UIimagePickerController with camera or Media Library according to user's choice.
here is some code:
import UIKit
protocol PFImagePickerProtocol: UIImagePickerControllerDelegate,UINavigationControllerDelegate where Self: UIViewController {
func didSelectImage(image: UIImage?, error: Bool)
func didCancelledImageSelection()
}
extension PFImagePickerProtocol {
func openImageSelector(withCorp cropEnabled:Bool) {
let alertController = UIAlertController(title: "Action Sheet", message: "What would you like to do?", preferredStyle: .actionSheet)
let camera = UIAlertAction(title: "Camera", style: .default) { (action) in
self.openImagePicker(withCorp: cropEnabled, sourceType: .camera)
}
let library = UIAlertAction(title: "Photo Library", style: .default) { (action) in
self.openImagePicker(withCorp: cropEnabled, sourceType: .photoLibrary)
}
alertController.addAction(camera)
alertController.addAction(library)
self.present(alertController, animated: true, completion: nil)
}
private func openImagePicker(withCorp cropEnabled:Bool, sourceType: UIImagePickerController.SourceType) {
let pickerVc = UIImagePickerController()
pickerVc.allowsEditing = cropEnabled
pickerVc.sourceType = sourceType
pickerVc.delegate = self //is this the problem?
self.present(pickerVc, animated: true, completion: nil)
}
}
extension PFImagePickerProtocol{
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
didCancelledImageSelection()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
didSelectImage(image: image, error: false)
return
} else {
didSelectImage(image: nil, error: true)
}
self.dismiss(animated: true, completion: nil)
}
}
as I run the code. function 'didFinishPickingMediaWithInfo' is not called.
I found this answer useful. but if there anything that can solved this problem. kindly share it here.
feel free to comment on code.
Please write your code as below:
protocol PFImagePickerProtocol {
func didSelectImage(image: UIImage?, error: Bool)
func didCancelledImageSelection()
}
And write your extensions as below that contains the delegate methods:
extension YourViewController {
func openImageSelector(withCorp cropEnabled:Bool) {
let alertController = UIAlertController(title: "Action Sheet", message: "What would you like to do?", preferredStyle: .actionSheet)
let camera = UIAlertAction(title: "Camera", style: .default) { (action) in
self.openImagePicker(withCorp: cropEnabled, sourceType: .camera)
}
let library = UIAlertAction(title: "Photo Library", style: .default) { (action) in
self.openImagePicker(withCorp: cropEnabled, sourceType: .photoLibrary)
}
alertController.addAction(camera)
alertController.addAction(library)
self.present(alertController, animated: true, completion: nil)
}
private func openImagePicker(withCorp cropEnabled:Bool, sourceType: UIImagePickerController.SourceType) {
let pickerVc = UIImagePickerController()
pickerVc.allowsEditing = cropEnabled
pickerVc.sourceType = sourceType
pickerVc.delegate = self //This will set your picker delegate to view controller class & the below extension conforms the delegates.
self.present(pickerVc, animated: true, completion: nil)
}
}
extension YourViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
didCancelledImageSelection()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
didSelectImage(image: image, error: false)
return
} else {
didSelectImage(image: nil, error: true)
}
self.dismiss(animated: true, completion: nil)
}
}
I think then you can write all of the above code in one single view controller like AbstractViewController which is a sub class of UIViewController, and all of your other view controllers that have this functionality have their super class as AbstractViewController.
class AbstractViewController: UIViewController {
// Do above code here
}
class OtherViewController: AbstractViewController {
// Classes that needs to implement the image picker functionality
}
class PFImagePickerManager
typealias PFImagePickerTarget = UIImagePickerControllerDelegate & UINavigationControllerDelegate
class PFImagePickerManager {
static var shared: PFImagePickerManager = PFImagePickerManager()
var target: PFImagePickerTarget!
private init() {}
func openImageSelector(target: PFImagePickerTarget, shouldCrop: Bool) {
self.target = target
let alertController = UIAlertController(title: PFConstants.PFImagePicker.actionSheetTitle, message: kEmptyStr, preferredStyle: .actionSheet)
let camera = UIAlertAction(title: PFConstants.PFImagePicker.camera, style: .default) { (action) in
self.openImagePicker(withCorp: shouldCrop, sourceType: .camera)
}
let library = UIAlertAction(title: PFConstants.PFImagePicker.photoLibrary, style: .default) { (action) in
self.openImagePicker(withCorp: shouldCrop, sourceType: .photoLibrary)
}
let cancel = UIAlertAction(title: PFConstants.PFImagePicker.cancel, style: .cancel) { (action) in
}
alertController.addAction(camera)
alertController.addAction(library)
alertController.addAction(cancel)
if let vc = target as? PFBaseViewController {
vc.present(alertController, animated: true, completion: nil)
}
}
private func openImagePicker(withCorp cropEnabled:Bool, sourceType: UIImagePickerController.SourceType) {
let pickerVc = UIImagePickerController()
pickerVc.allowsEditing = cropEnabled
pickerVc.sourceType = sourceType
pickerVc.delegate = target
if sourceType == .photoLibrary {
pickerVc.navigationBar.tintColor = UIColor.appThemePrimaryColor()
}
if let vc = target as? PFBaseViewController {
vc.present(pickerVc, animated: true, completion: nil)
}
}
}
you need to extend PFImagePickerTarget
extension YourViewController: PFImagePickerTarget {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
PFViewUtility.dispatchOnMainThread {
self.VMCreateGR.changeDatafor(.image, data: image)
self.tblInputs.reloadData()
}
} else {
self.view.makeToast("Error while selecting image. Please try again.")
}
picker.dismiss(animated: true, completion: nil)
}
}
and to initiate image picker in ViewController
class AnyViewController: UIViewController {
// In some method like viewDidLoad call the below line
PFImagePickerManager.shared.openImageSelector(target: self, shouldCrop: true)
}

UIImagePickerController didFinishPickingMediaWithInfo not being called with cameraOverlyView

I have UIImagePickerController with cameraOverlyView. I added tap gesture recognizer to the overlay view - when user taps on the view the recording should be stopped and saved:
let mediaUI = UIImagePickerController()
mediaUI.sourceType = sourceType
mediaUI.mediaTypes = [kUTTypeMovie as String]
mediaUI.allowsEditing = true
mediaUI.delegate = self
//customView stuff
let customViewController = CustomOverlayViewController(
nibName:"CustomOverlayViewController",
bundle: nil
)
let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
customView.frame = delegate.view.frame //self.picker.view.frame
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraViewTapped(tapGestureRecognizer:)))
customView.cameraView.addGestureRecognizer(tapGestureRecognizer)
customView.delegate = delegate
delegate.present(mediaUI, animated: true, completion: {
mediaUI.cameraOverlayView = customView
})
gesture recognizer calls this:
#objc func cameraViewTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
if isRecording {
self.picker!.stopVideoCapture()
self.recordButton.isEnabled = false
print("stopped")
isRecording = false
} else {
self.picker!.startVideoCapture()
print("started")
isRecording = true
}
}
delegate method:
extension HomeViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
print("didFinishPickingMediaWithInfo")
dismiss(animated: true, completion: nil)
guard
let mediaType = info[UIImagePickerControllerMediaType] as? String,
mediaType == (kUTTypeMovie as String),
let url = info[UIImagePickerControllerMediaURL] as? URL,
UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path)
else {
return
}
// Handle a movie capture
UISaveVideoAtPathToSavedPhotosAlbum(
url.path,
self,
#selector(video(_:didFinishSavingWithError:contextInfo:)),
nil)
}
}
My question is how to get recording from the picker controller? Delegate method didFinishPickingMediaWithInfo is not called because of the overlay view: UIImagePickerController didFinishPickingMediaWithInfo not being called when cameraOverlyView property is assigned
Solved the issue by adding the following line of code:
mediaUI.showsCameraControls = false

Show a view in front of the imagePicker

In my application the user can either open the camera roll to select a picture or open the camera to take directly one by himself.
In both cases, the picture selected/taken will also be saved locally for further reference.
The downside is that the saving operation usually freeze the screen until it is finished.
I found an animation in this post and I want to display it in front of the imagePickerController but I can't manage to do so.
class SinglePageViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate, UINavigationBarDelegate {
var spinner: UIActivityIndicatorView?
lazy var showCameraImagePickerController: UIImagePickerController = {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
return imagePicker
}()
lazy var showPhotoImagePickerController: UIImagePickerController = {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
return imagePicker
}()
#IBOutlet weak var photoButton: UIButton!
#IBAction func onPhotoButton(_ sender: Any) {
self.present(self.showCameraImagePickerController, animated: true, completion: nil)
}
#IBOutlet weak var galleryButton: UIButton!
#IBAction func onGalleryButton(_ sender: Any) {
self.present(self.showPhotoImagePickerController, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
//start animation
let screenSize: CGRect = UIScreen.main.bounds
spinner = UIActivityIndicatorView(frame: CGRect(x: screenSize.width / 2 - 150, y: screenSize.height / 2 - 150, width: 300, height: 300))
spinner?.isHidden = false
spinner?.startAnimating()
spinner?.color = UIColor.red
switch picker {
case showCameraImagePickerController:
// snap pic, save to doc, save to album
self.showCameraImagePickerController.view.addSubview(spinner!)
timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false, block: { _ in
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
if self.saveImage(imageName: "\(self.titleLabel.text!).png", image: image) {
// additionally save to photo album
UIImageWriteToSavedPhotosAlbum(image!, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
print("saved \(self.titleLabel.text!).png")
self.imageView.image = image
}
})
case showPhotoImagePickerController:
//switch pic, save to doc. no album
self.showPhotoImagePickerController.view.addSubview(spinner!)
timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false, block: { _ in
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
if self.saveImage(imageName: "\(self.titleLabel.text!).png", image: image) {
print("saved new \(self.titleLabel.text!).png")
self.imageView.image = image
self.spinner?.stopAnimating()
self.spinner?.removeFromSuperview()
self.spinner = nil
self.showPhotoImagePickerController.dismiss(animated: true, completion: nil)
} else {
self.spinner?.stopAnimating()
self.spinner?.removeFromSuperview()
self.spinner = nil
self.showPhotoImagePickerController.dismiss(animated: true, completion: nil)
}
})
default:
return
}
}
#objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
spinner?.stopAnimating()
spinner?.removeFromSuperview()
spinner = nil
self.showCameraImagePickerController.dismiss(animated: true, completion: nil)
}
func saveImage(imageName: String, image: UIImage?) -> Bool {
//create an instance of the FileManager
let fileManager = FileManager.default
//get the image path
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imgDir + imageName)
print(imagePath)
//get the image we took with camera
let image = rotateImage(image: image!)
//get the PNG data for this image
let data = UIImagePNGRepresentation(image)
//store it in the document directory
if fileManager.createFile(atPath: imagePath as String, contents: data, attributes: nil) {
newItem?.image = true
return true
} else {
print("error while saving")
return false
}
}
}
as you can see I tried playing with bringSubView(toFront:) and also with the zPosition but with no results.
following this similar question I looked into the documentation for cameraOverlayView but it says that it only works when the imagePicker is presented in camera mode, which doesn't cover the case when I open the photo library
I also recently tried to use a workaround, meaning that I dismiss the imagePickerController as soon as possible and update the image afterwards, but that is not optimal anymore because of some changes in the structure of app.
EDIT
to make myself clearer I'll state again what I need: show the spinner animation in front of the imagePicker, as soon as I tap a photo to choose it, and until I finish saving, then dismiss the imagePicker.
I do not want to first dismiss the picker and then save while showing the spinner in the main view.
EDIT2
updated the code with the new one from the answer. only problem is that if I don't put a timer the spinner shows itself only at the end of the saving process for a brief moment (checked with breakpoints).
This results in no animation during the couple of seconds of saving process and just a brief apparition of the spinner at the end before dismissing the imagePicker.
Just putting a 0.1sec delay triggers the spinner immediately and I get the expected behaviour (animation while saving).
No idea why
Please see a complete example where spinner will show while the image is being saved and once finishes saving spinner will be removed.
class ViewController: UIViewController {
/// Image picker controller
lazy var imagePickerController: UIImagePickerController = {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = false
return imagePicker
}()
var spinner: UIActivityIndicatorView?
#IBAction func imagePickerButton(_ sender: UIButton) {
self.present(self.imagePickerController, animated: true, completion: nil)
}
}
// MARK: ImagePicker Delegate to get the image picked by the user
extension ViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//start animation
let screenSize: CGRect = UIScreen.main.bounds
spinner = UIActivityIndicatorView(frame: CGRect(x: screenSize.width/2 - 50, y: screenSize.height/2 - 50, width: 100, height: 100))
spinner?.isHidden = false
spinner?.startAnimating()
spinner?.color = UIColor.black
self.imagePickerController.view.addSubview(spinner!)
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
UIImageWriteToSavedPhotosAlbum(image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
#objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
spinner?.stopAnimating()
spinner?.removeFromSuperview()
spinner = nil
self.imagePickerController.dismiss(animated: true, completion: nil)
if error == nil {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}

How to implement camera in swift

My app always crashes when I press the camera button. I am trying to get the camera to appear on my screen when I press the camera button.
#IBAction func ACPressed(_ sender: Any) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .camera
present(picker, animated: true, completion: nil)
}
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String :AnyObject] ){
camera.image = info [UIImagePickerControllerOriginalImage] as? UIImage; dismiss(animated: true, completion: nil)
}
This is probably a simulator issue. If you want to add code to handle this case, you can do the following:
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
By wrapping it in "isSourceTypeAvailable", it will avoid the crash. You can add an else statement in there and display an alert message notifying of the issue.
Don't use simulator to test the camera. Use real device.
Another way you will see the exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'
class ViewController:UIViewController, UIImagePickerControllerDelegate {
private var imagePicker : UIImagePickerController!
private var photo: UIImage?
#IBAction func ACPressed(_ sender: Any) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
photo = info[UIImagePickerControllerOriginalImage] as? UIImage
}
}
Don't forget to set "Privacy - Camera Usage Description" in your
Info.plist
I don't know why my answer is downvoted but try this code on the real device and you will see that the code works perfect!