Right now my code makes the user touch the screen 3 times before the photo is taken. What I am talking about is shown below.
PHOTO 1
PHOTO 2
PHOTO 3
PHOTO 4
I want the code to get rid of the middle to pictures. I want the user to go from pic 1 to pic 4 without the 2 and 3 pics. I have gotten bits and pieces of the code from other places so I am just trying to the user take the photo as efficiently as possible. My code is below. Thanks
import UIKit;import AVFoundation
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate, AVCapturePhotoCaptureDelegate {
@IBOutlet var vieview: UIView!;@IBOutlet var bbButon: UIButton!
var screenView: UIImageView!;@IBOutlet var theImageVIEWTODISPALY: UIImageView!
@IBOutlet var button2: UIButton!;var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput();var previewLayer = AVCaptureVideoPreviewLayer()
@IBOutlet var takePhotoButton: NSLayoutConstraint!
@IBOutlet var takePhotoButton2: NSLayoutConstraint!
@IBOutlet var lx3: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()}
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let deviceSession = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInDualCamera,.builtInTelephotoCamera,.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .unspecified)
for device in (deviceSession?.devices)! {
if device.position == AVCaptureDevicePosition.back {
do {
let input = try AVCaptureDeviceInput(device: device)
if captureSession.canAddInput(input){
captureSession.addInput(input)
if captureSession.canAddOutput(sessionOutput){
captureSession.addOutput(sessionOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = .portrait
vieview.layer.addSublayer(previewLayer)
vieview.addSubview(bbButon)
vieview.addSubview(button2)
previewLayer.position = CGPoint(x: self.vieview.frame.width / 2, y: self.vieview.frame.width / 2)
previewLayer.bounds = vieview.frame
captureSession.startRunning()
}}
} catch let avError {
print(avError)
}}}}
@IBAction func takingPhoto(_ sender: Any) {
UIGraphicsBeginImageContext(self.theImageVIEWTODISPALY.frame.size)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
previewLayer.isHidden = true
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func savingPhoto(_ sender: Any) {
let alert = UIAlertController(title: "Image Saved", message: "Image is in photo gallery", preferredStyle: .alert)
let okay = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okay)
present(alert, animated: true, completion: nil)
let photo = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(photo!, nil, nil, nil)}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject: AnyObject]!){
theImageVIEWTODISPALY.image = image
self.dismiss(animated: true, completion: nil);
screenView = UIImageView()
screenView.frame = CGRect(x:0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
self.view.addSubview(screenView)
self.view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true)
}}