为什么当 UIImagePickerController 处于视频模式时,AVFoundation 显示与 UIImagePickerController 相同的缩放级别?
Why does AVFoundation display at the same zoom level as UIImagePickerController when UIImagePickerController is in video mode?
为什么当 UIImagePickerController 处于视频模式时,AVFoundation 显示与 UIImagePickerController 相同的缩放级别?如何让 AVFoundation 在照片模式下以与 UIImagePickerController 相同的缩放级别显示?
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
}
}
两个视图的纵横比不同。顶视图用视频填充屏幕,并且剪辑了一些视频。而第二个视频没有。一般来说,视频的标准输出是 4:3 纵横比。您可以通过枚举 AVCaptureDeviceFormat 或您的 AVCaptureDevice 来验证这一点。调整 cameraView 的大小,使其具有相同的纵横比,并且您应该获得相同的缩放级别。
为什么当 UIImagePickerController 处于视频模式时,AVFoundation 显示与 UIImagePickerController 相同的缩放级别?如何让 AVFoundation 在照片模式下以与 UIImagePickerController 相同的缩放级别显示?
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
}
}
两个视图的纵横比不同。顶视图用视频填充屏幕,并且剪辑了一些视频。而第二个视频没有。一般来说,视频的标准输出是 4:3 纵横比。您可以通过枚举 AVCaptureDeviceFormat 或您的 AVCaptureDevice 来验证这一点。调整 cameraView 的大小,使其具有相同的纵横比,并且您应该获得相同的缩放级别。