I am utilizing avfoundation to make a recording app, my portrait works nicely however every time I rotates, the preview rotates too (When my cellphone goes from portrait to panorama proper, ‘v’ turns into ‘<‘).
I attempted utilizing a UIViewController and a UIViewControllerRepresentable, I attempted placing updatePreviewLayerOrientation() in viewDidLoad(), viewDidLayoutSubviews(), viewWillTransition(), none of them work. The preview rotates and zooms in (I feel it is because of resizeAspectFill)
class VideoPreviewView: UIView {
override class var layerClass: AnyClass {
return AVCaptureVideoPreviewLayer.self
}
var videoPreviewLayer: AVCaptureVideoPreviewLayer {
return layer as! AVCaptureVideoPreviewLayer
}
}
class CameraViewController: UIViewController {
personal var previewView = VideoPreviewView()
override func viewDidLoad() {
tremendous.viewDidLoad()
previewView.videoPreviewLayer.session = CameraManager.shared.session
previewView.videoPreviewLayer.videoGravity = .resizeAspectFill
previewView.videoPreviewLayer.body = view.bounds
view.layer.addSublayer(previewView.videoPreviewLayer)
updatePreviewLayerOrientation
}
override func viewDidLayoutSubviews() {
tremendous.viewDidLayoutSubviews()
}
func updatePreviewLayerOrientation() {
guard let connection = previewView.videoPreviewLayer.connection else { return }
change UIDevice.present.orientation {
case .portrait:
connection.videoOrientation = .portrait
case .landscapeRight:
connection.videoOrientation = .landscapeLeft
case .landscapeLeft:
connection.videoOrientation = .landscapeRight
case .portraitUpsideDown:
connection.videoOrientation = .portraitUpsideDown
default:
connection.videoOrientation = .portrait
}
previewView.videoPreviewLayer.body = view.bounds
}
then I modified to a UIViewRepresentable. Once more I attempted calling updatePreviewLayerOrientation() in updateUIView, in updateUIView, do not work.
struct CameraViewRepresentable: UIViewRepresentable {
func makeUIView(context: Context) -> VideoPreviewView {
let view = VideoPreviewView()
view.backgroundColor = .black
view.videoPreviewLayer.session = CameraManager.shared.session
view.videoPreviewLayer.videoGravity = .resizeAspectFill
CameraManager.shared.videoPreview = view
return view
}
public func updateUIView(_ uiView: VideoPreviewView, context: Context) { }
class VideoPreviewView: UIView {
override class var layerClass: AnyClass {
AVCaptureVideoPreviewLayer.self
}
var videoPreviewLayer: AVCaptureVideoPreviewLayer {
return layer as! AVCaptureVideoPreviewLayer
}
}
}
I attempted writing a devideRotationModifier and replace orientation there, does not work.
struct DeviceRotationViewModifier: ViewModifier {
let motion: (UIDeviceOrientation) -> Void
func physique(content material: Content material) -> some View {
content material
.onAppear()
.onReceive(NotificationCenter.default.writer(for: UIDevice.orientationDidChangeNotification)) { _ in
motion(UIDevice.present.orientation)
}
}
}
// A View wrapper to make the modifier simpler to make use of
extension View {
func onRotate(carry out motion: @escaping (UIDeviceOrientation) -> Void) -> some View {
self.modifier(DeviceRotationViewModifier(motion: motion))
}
}
What’s the right method to set the orientations?