我的应用程序中有一个可以改变方向的相机。 最初,相机方向是从当前设备方向设置的。
captureSession.addOutput(videoOutput!)
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) ?? .portrait
旋转时,我改变了相机方向。
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: nil) { _ in UIView.setAnimationsEnabled(true) }
UIView.setAnimationsEnabled(false)
super.viewWillTransition(to: size, with: coordinator)
Logger.Log("VIEW WILL TRANSITION")
if let videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) {
Logger.Log("videoOrientation updated")
cameraPreviewLayer?.connection?.videoOrientation = videoOrientation
}
cameraPreviewLayer?.frame.size = size
}
录制完成后,我将视频URL传输到AVPlayer。你可以看到播放器以错误的方向播放视频。
if let _ = link {
let player = AVPlayer(url: link!)
self.player = player
self.player?.play()
}
例如,这是我在横向方向上录制的方式。
我得到以下结果。虽然结果应该反过来看。
在服务器上,它也以错误的方向存储。
我看了this question。但似乎我在录制时已经指定了方向。
我也看过this question。我试着看看播放器播放的视频在不同方向上是否有所不同。但我总能得到相同的结果。
func playLink(){
if let _ = link {
let player = AVPlayer(url: link!)
self.player = player
self.player?.play()
}
let videoAssetTrack = self.player?.currentItem?.asset.tracks(withMediaType: .video).first
let videoTransform = videoAssetTrack?.preferredTransform
Logger.Log("videoTransform = \(videoTransform)")
}
videoTransform = Optional(__ C.CGAffineTransform(a:0.0,b:1.0,c:-1.0,d:0.0,tx:1080.0,ty:0.0))
请帮帮我,我该怎么做才能在输出上得到正确的结果?
显然,起初我在this answer做错了。
我刚拿出connection
和orientation
作为全班的全球领域并解决了它们。
现在我添加了以下代码,这解决了我的问题。
var currentOrientation: AVCaptureVideoOrientation?
var videoConnection: AVCaptureConnection?
captureSession.addOutput(videoOutput!)
videoConnection = videoOutput!.connection(with: .video)
if (videoConnection!.isVideoOrientationSupported) {
Logger.Log("connection!.isVideoOrientationSupported")
if let capOr = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) {
Logger.Log("connection!.videoOrientation = \(capOr.rawValue)")
currentOrientation = capOr
videoConnection!.videoOrientation = currentOrientation!
} else {
currentOrientation = .portrait
videoConnection!.videoOrientation = currentOrientation!
}
}
cameraPreviewLayer?.connection?.videoOrientation = currentOrientation!
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: nil) { _ in UIView.setAnimationsEnabled(true) }
UIView.setAnimationsEnabled(false)
super.viewWillTransition(to: size, with: coordinator)
Logger.Log("VIEW WILL TRANSITION")
if let videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) {
Logger.Log("videoOrientation updated = \(videoOrientation.rawValue)")
currentOrientation = videoOrientation
videoConnection?.videoOrientation = currentOrientation!
cameraPreviewLayer?.connection?.videoOrientation = currentOrientation!
}
cameraPreviewLayer?.frame.size = size
}