如何设置AVFoundation输出的照片为Portrait
2019-01-02 本文已影响2人
南国青天
let device = AVCaptureDevice.devices().filter { $0.hasMediaType(AVMediaType.video) && $0.position == .front }.first!
let input:AVCaptureDeviceInput
do {
input = try AVCaptureDeviceInput(device: device)
} catch {
return
}
let output = AVCaptureVideoDataOutput()
captureSession = AVCaptureSession()
captureSession?.addInput(input)
//先将输出添加到到captureSesssion,然后才能设置connection, 否则输出照片无法Portrait。
captureSession?.addOutput(output)
output.connection(with: .video)?.videoOrientation = .portrait
output.connection(with: .video)?.isVideoMirrored = true
let queue = DispatchQueue(label: "cameraQueue")
output.setSampleBufferDelegate(self, queue: queue)
output.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String: kCVPixelFormatType_32BGRA]
captureSession?.startRunning()