I use AVFoundation to take photo but i need taked photo be 1920x1440 or if this resolution not supported on device i need to image be 4:3 aspect ratio. How i do this? My code for camera:
Camera initializer:
func initCamera()
{
captureSession = AVCaptureSession()
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer.frame = self.cameraView.frame
videoPreviewLayer.videoGravity = .resizeAspectFill
guard let backCamera = AVCaptureDevice.default(for: AVMediaType.video) else {
print("Unable to access back camera!")
return
}
backCamera.formats
self.chaprerDevice = backCamera
do {
cameraInput = try AVCaptureDeviceInput(device: backCamera)
stillImageOutput = AVCapturePhotoOutput()
if captureSession.canAddInput(cameraInput) && captureSession.canAddOutput(stillImageOutput) {
captureSession?.beginConfiguration()
captureSession.sessionPreset = .photo
captureSession.addInput(cameraInput)
captureSession.addOutput(stillImageOutput)
captureSession?.commitConfiguration()
setupLivePreview()
}
} catch let error {
print("Error Unable to initialize back camera: (error.localizedDescription)")
}
}
func setupLivePreview() {
videoPreviewLayer.connection?.videoOrientation = .landscapeRight
self.cameraView.layer.addSublayer(videoPreviewLayer)
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
self?.captureSession.startRunning()
DispatchQueue.main.async {
self?.videoPreviewLayer.frame = self?.cameraView.bounds ?? UIScreen.main.bounds
}
}
}
Function called when to take a picture:
func takePhoto {
let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey:AVVideoCodecType.jpeg])
settings.isAutoStillImageStabilizationEnabled = true
settings.flashMode = flash
let pbpf = settings.__availablePreviewPhotoPixelFormatTypes[0]
settings.previewPhotoFormat = [
kCVPixelBufferPixelFormatTypeKey as String : pbpf,
kCVPixelBufferWidthKey as String : 1920,
kCVPixelBufferHeightKey as String : 1440]
stillImageOutput.capturePhoto(with: settings, delegate: self)
}
question from:
https://stackoverflow.com/questions/65887026/camera-avfondation-set-aspect-ration-or-resolution 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…