Ios 根据设备方向旋转视频预览层。Xcode Swift
我一直在尝试让我的实时视频提要(相机预览层)在设备旋转时旋转方向。我完全卡住了。我在下面提供了我当前的代码 注意:此应用程序是iMessage扩展,不是普通应用程序 我当前的代码:Ios 根据设备方向旋转视频预览层。Xcode Swift,ios,swift,xcode,avfoundation,orientation,Ios,Swift,Xcode,Avfoundation,Orientation,我一直在尝试让我的实时视频提要(相机预览层)在设备旋转时旋转方向。我完全卡住了。我在下面提供了我当前的代码 注意:此应用程序是iMessage扩展,不是普通应用程序 我当前的代码: func setupPreviewLayer() { cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) cameraPreviewLayer?.videoGravity = AVLayer
func setupPreviewLayer() {
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
orientationChange()
cameraPreviewLayer?.frame.size.height = self.view.frame.size.height / 2
cameraPreviewLayer?.frame.size.width = self.view.frame.size.width / 2
liveVideoFeedPosition()
cameraPreviewLayer?.position.x = self.view.frame.width / 2
cameraPreviewLayer?.position.y = self.view.frame.height / 4 //- 33 original
self.view.layer.addSublayer(cameraPreviewLayer!)
}
**不连续
func liveVideoFeedPosition() {
if presentationStyle == MSMessagesAppPresentationStyle.compact {
holoLogo.isHidden = true
cameraPreviewLayer?.position.x = self.view.frame.width / 2
cameraPreviewLayer?.position.y = self.view.frame.height / 4 //+33 original
}
if presentationStyle == MSMessagesAppPresentationStyle.expanded {
holoLogo.isHidden = false
cameraPreviewLayer?.position.x = self.view.frame.width / 2
cameraPreviewLayer?.position.y = self.view.frame.height / 2 //- 27 original
}
self.OutlineImage.center.y = self.view.center.y // - 27 original
self.OutlineImage.center.x = self.view.center.x
self.view.addSubview(OutlineImage)
}
**不连续
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
if UIDevice.current.orientation == .portrait {
captureSession.stopRunning()
self.view.layer.sublayers?.removeAll()
orientation = "Portrait"
setupInputOutput()
setupPreviewLayer()
captureSession.startRunning()
}
if UIDevice.current.orientation == .portraitUpsideDown {
captureSession.stopRunning()
self.view.layer.sublayers?.removeAll()
orientation = "Portrait UpsideDown"
setupInputOutput()
setupPreviewLayer()
captureSession.startRunning()
}
if UIDevice.current.orientation == .landscapeLeft {
captureSession.stopRunning()
self.view.layer.sublayers?.removeAll()
orientation = "Landscape Left"
setupInputOutput()
setupPreviewLayer()
captureSession.startRunning()
}
if UIDevice.current.orientation == .landscapeRight {
captureSession.stopRunning()
self.view.layer.sublayers?.removeAll()
orientation = "Landscape Right"
setupInputOutput()
setupPreviewLayer()
captureSession.startRunning()
}
liveVideoFeedPosition()
}
var orientation = "Portrait"
func orientationChange() {
if orientation == "Portrait" {
cameraPreviewLayer?.connection?.videoOrientation = .portrait
}
if orientation == "Portrait UpsideDown" {
cameraPreviewLayer?.connection?.videoOrientation = .portraitUpsideDown
}
if orientation == "Landscape Right" {
cameraPreviewLayer?.connection?.videoOrientation = .landscapeRight
}
if orientation == "Landscape Left" {
cameraPreviewLayer?.connection?.videoOrientation = .landscapeLeft
}
}
**不连续
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
if UIDevice.current.orientation == .portrait {
captureSession.stopRunning()
self.view.layer.sublayers?.removeAll()
orientation = "Portrait"
setupInputOutput()
setupPreviewLayer()
captureSession.startRunning()
}
if UIDevice.current.orientation == .portraitUpsideDown {
captureSession.stopRunning()
self.view.layer.sublayers?.removeAll()
orientation = "Portrait UpsideDown"
setupInputOutput()
setupPreviewLayer()
captureSession.startRunning()
}
if UIDevice.current.orientation == .landscapeLeft {
captureSession.stopRunning()
self.view.layer.sublayers?.removeAll()
orientation = "Landscape Left"
setupInputOutput()
setupPreviewLayer()
captureSession.startRunning()
}
if UIDevice.current.orientation == .landscapeRight {
captureSession.stopRunning()
self.view.layer.sublayers?.removeAll()
orientation = "Landscape Right"
setupInputOutput()
setupPreviewLayer()
captureSession.startRunning()
}
liveVideoFeedPosition()
}
var orientation = "Portrait"
func orientationChange() {
if orientation == "Portrait" {
cameraPreviewLayer?.connection?.videoOrientation = .portrait
}
if orientation == "Portrait UpsideDown" {
cameraPreviewLayer?.connection?.videoOrientation = .portraitUpsideDown
}
if orientation == "Landscape Right" {
cameraPreviewLayer?.connection?.videoOrientation = .landscapeRight
}
if orientation == "Landscape Left" {
cameraPreviewLayer?.connection?.videoOrientation = .landscapeLeft
}
}
所有这些代码都不是一个特定的顺序。我不想表现得粗鲁或其他什么,但如果你不明白,你可以尝试设置一个特定的方向。@E.Huckabee这是我最初的计划。因为它是iMessage扩展,所以必须支持所有方向。如果我在应用程序第一次启动时设置方向,它会工作,但如果设备旋转,它不会改变。这就像我设置后无法更改它一样。很抱歉,我没有理解它是iMessage扩展的部分。我会调查一下,看看我能做些什么。