Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement screen rotation #306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,22 @@ class CameraFeedManager: NSObject {

}
}

// MARK: Video orientation
/**
This method rotates the video flowing through the connection based on a new interface orientation
*/
func changeVideoOrientation() {
guard previewView.previewLayer.connection!.isVideoOrientationSupported,
let newOrientation = UIApplication.shared.statusBarOrientation.videoOrientation else {
return
}

previewView.previewLayer.connection?.videoOrientation = newOrientation
videoDataOutput.connection(with: .video)?.videoOrientation = newOrientation
}
}


/**
AVCaptureVideoDataOutputSampleBufferDelegate
*/
Expand All @@ -344,3 +357,17 @@ extension CameraFeedManager: AVCaptureVideoDataOutputSampleBufferDelegate {
}

}

extension UIInterfaceOrientation {

/// Returns `AVCaptureVideoOrientation` based on `UIInterfaceOrientation`
var videoOrientation: AVCaptureVideoOrientation? {
switch self {
case .portraitUpsideDown: return .portraitUpsideDown
case .landscapeRight: return .landscapeRight
case .landscapeLeft: return .landscapeLeft
case .portrait: return .portrait
default: return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,15 @@ extension ViewController {
}

}

extension ViewController {
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

coordinator.animate(alongsideTransition: nil, completion: { [weak self] (context) in
DispatchQueue.main.async(execute: {
self?.cameraFeedManager.changeVideoOrientation()
})
})
}
}