Why does AVFoundation display at the same zoom level as UIImagePickerController when UIImagePickerController is in video mode? How do I make AVFoundation display at the same zoom level as UIImagePickerController in photo mode?
import UIKit
import AVFoundation
class ViewController: UIViewController {
var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput()
var sessionOutputSetting = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecJPEG])
var previewLayer = AVCaptureVideoPreviewLayer()
var cameraView: UIView?
override func viewDidLoad() {
cameraView = UIView(frame: view.frame)
view.addSubview(cameraView!)
}
override func viewWillAppear(_ animated: Bool) {
let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInDualCamera, .builtInTelephotoCamera, .builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .unspecified)
for device in (deviceDiscoverySession?.devices)! {
if device.position == .back {
do {
let input = try AVCaptureDeviceInput(device: device)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
if captureSession.canAddOutput(sessionOutput) {
captureSession.addOutput(sessionOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = (cameraView?.bounds)!
previewLayer.videoGravity = AVLayerVideoGravityResizeAspect // AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = .portrait
cameraView?.layer.addSublayer(previewLayer)
}
}
} catch {
print("exception!")
}
}
}
}
override func viewDidAppear(_ animated: Bool) {
captureSession.startRunning()
}
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
The aspect ratio for the two views are different. The top view fills the screen with the video and some of the video is clipped. While the second one the video doesn't. In general the standard output for video is 4:3 aspect ratio. You can verify that by enumerating the AVCaptureDeviceFormat or your AVCaptureDevice. Size your cameraView so it has the same aspect ratio and you should get the same zoom level.