i’ve an issue for setting my digital camera app vast angle and extremely vast angle choices , i exploit phase management for change angle:
import UIKit
import AVFoundation
import Pictures
import CoreMotion
class cameraViewController: UIViewController, AVCapturePhotoCaptureDelegate, UIGestureRecognizerDelegate {
var cameraView: UIView!
var captureButton: UIButton!
var cameraSwitchButton: UIButton!
var flashModeButton: UIButton!
//var backCamera: AVCaptureDevice!
var currentCamera: AVCaptureDevice?
var captureSession: AVCaptureSession!
var stillImageOutput: AVCapturePhotoOutput!
var previewLayer: AVCaptureVideoPreviewLayer!
var currentZoomFactor: CGFloat = 1.0
var maxZoomFactor: CGFloat = 5.0
var zoomStep: CGFloat = 1.0
var currentFlashMode: AVCaptureDevice.FlashMode = .auto // set flash mode on auto
var isFlashOn: Bool = false
var exposurePoint = CGPoint(x: 0.5, y: 0.5)
var focusPoint = CGPoint(x: 0.5, y: 0.5)
var boxView: UIView!
var exposureSlider: UISlider!
var currentExposureBias: Float = 0.0
var currentFocusLevel: Float = 0.0
var motionManager: CMMotionManager!
var yawAngle: Double = 0.0
var aspectControll : UISegmentedControl! // phase controll for choose a side ratio
var currentAspectRatio: AVCaptureSession.Preset = .hd1920x1080 // set default aspet ratio on seize session
var timerButton : UIButton!
var timerDuration: TimeInterval = 0
var timerLabel: UILabel!
var timer : Timer?
var styleButton : UIButton!
var levelView: UIView!
var tolerance : CGFloat = 4.0
var straightAngle: CGFloat = 0.0
var wideControll : UISegmentedControl!
override func viewDidLoad() {
tremendous.viewDidLoad()
setupCameraView()
setupCamera()
setupButtons()
setupBoxView()
setupExposureSlider()
setupGestures()
setupMotionManager()
setupLevelView()
let objects = ["Wide Angle", "Ultra Wide Angle"]
wideControll = UISegmentedControl(objects: objects)
wideControll?.body = CGRect(x: 50, y: 50, width: 300, peak: 50)
wideControll?.selectedSegmentIndex = 0 // Set the default chosen phase to "Extensive Angle"
// wideControll?.addTarget(self, motion: #selector(wideAngleValueChanged(_:)), for: .valueChanged)
view.addSubview(wideControll!)
}
//it was utilizing for cover standing bar on show eg:battery standing,singnal
override var prefersStatusBarHidden: Bool{
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override func viewDidAppear(_ animated: Bool) {
tremendous.viewDidAppear(animated)
previewLayer?.connection?.videoOrientation = currentVideoOrientation()
previewLayer?.body = cameraView.bounds
// previewLayer.videoGravity = .resizeAspect
}
override func viewWillTransition(to measurement: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
tremendous.viewWillTransition(to: measurement, with: coordinator)
coordinator.animate(alongsideTransition: { _ in
self.previewLayer?.connection?.videoOrientation = self.currentVideoOrientation()
self.previewLayer?.body = CGRect(x: 0, y: 0, width: measurement.width, peak: measurement.peak)
}, completion: { _ in
// Completion handler if wanted
})
}
override func viewWillAppear(_ animated: Bool) {
tremendous.viewWillAppear(animated)
captureSession.startRunning()
}
override func viewWillDisappear(_ animated: Bool) {
tremendous.viewWillDisappear(animated)
captureSession.stopRunning()
}
func toggleButtonsVisibility(_ shouldHide: Bool) {
DispatchQueue.predominant.async {
self.flashModeButton.isHidden = shouldHide
self.aspectControll.isHidden = shouldHide
self.timerButton.isHidden = shouldHide
self.timerLabel.isHidden = shouldHide
self.styleButton.isHidden = shouldHide
}
}
func setupCameraView() {
cameraView = UIView()
cameraView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(cameraView)
NSLayoutConstraint.activate([
cameraView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
cameraView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
cameraView.topAnchor.constraint(equalTo: view.topAnchor),
cameraView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
func setupLevelView() {
levelView = UIView(body: CGRect(x: 0, y: 0, width: cameraView.body.width, peak: 2))
levelView.backgroundColor = UIColor.purple
cameraView.addSubview(levelView)
//levelView.translatesAutoresizingMaskIntoConstraints = false
// Including heart constraints for the levelView
NSLayoutConstraint.activate([
levelView.centerXAnchor.constraint(equalTo: cameraView.centerXAnchor),
levelView.centerYAnchor.constraint(equalTo: cameraView.centerYAnchor),
levelView.widthAnchor.constraint(equalTo: cameraView.widthAnchor, multiplier: 1.0),
levelView.heightAnchor.constraint(equalToConstant: 2)
])
print("Degree view body: (levelView.body)")
print("Degree view bounds: (levelView.bounds)")
}
func setupCamera() {
captureSession = AVCaptureSession()
captureSession.sessionPreset = currentAspectRatio
if let gadget = AVCaptureDevice.default(.builtInUltraWideCamera, for: .video, place: .again){
// backCamera = gadget
do {
let enter = strive AVCaptureDeviceInput(gadget: gadget)
if captureSession.canAddInput(enter) {
captureSession.addInput(enter)
}
stillImageOutput = AVCapturePhotoOutput() // Initialize it as an AVCapturePhotoOutput
if captureSession.canAddOutput(stillImageOutput) { // Unwrap the non-compulsory
captureSession.addOutput(stillImageOutput)
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraView?.layer.addSublayer(previewLayer)
DispatchQueue.world(qos: .userInitiated).async {
self.captureSession.startRunning()
}
} catch {
print("Error establishing digital camera: (error)")
}
}
}