Так как вы используете AVAssetWriter
.Попробуйте,
private let assetWriter: AVAssetWriter
private var adaptor: AVAssetWriterInputPixelBufferAdaptor
И инициализируйте AVAssetWriter
следующим образом:
adaptor = AVAssetWriterInputPixelBufferAdaptor(rotationAngle: AVCaptureDevice.correctOrientation)
assetWriter = AVAssetWriter(input: adaptor.assetWriterInput)
Создайте extension
для AVCaptureDevice
, Измените углы соответственно для поворота.
// The angle by which to rotate captured media, based on the current orientation, so that it looks correctly oriented to the user.
var correctOrientation: CGFloat {
let angle: CGFloat
switch(UIDevice.current.orientation) {
case .portraitUpsideDown: angle = -CGFloat.pi / 2 // Play around with these values
case .landscapeLeft: angle = -CGFloat.pi
case .landscapeRight: angle = 0
case .portrait: angle = CGFloat.pi / 2
default: angle = CGFloat.pi / 2
}
return angle
}
Создать еще один extension
для AVAssetWriterInputPixelBufferAdaptor
extension AVAssetWriterInputPixelBufferAdaptor {
convenience init(rotationAngle: CGFloat) {
let input = AVAssetWriterInput(width: UIDevice.activeFormat.width, height: UIDevice.activeFormat.height)
input.transform = CGAffineTransform(rotationAngle: rotationAngle)
self.init(
assetWriterInput: input,
sourcePixelBufferAttributes: [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA, // use whatever format you used
kCVPixelBufferWidthKey as String: UIDevice.activeFormat.width,
kCVPixelBufferHeightKey as String: UIDevice.activeFormat.height])
}