How do I disable video stabilization in a AVCaptureSession with AVCapturePhotoOutput added?

I need to capture 4k photos with 4:3 ratio from the camera. I can do this, but i want to disable video stabilization. I can disable video stabilization using the AVCaptureSessionPresetHigh preset. But AVCaptureSessionPresetHigh gives me a 16:9 photo with the surroundings cropped. Unfortunately, the 16:9 ratio does not solve my needs.

When I run the session using the AVCaptureSessionPresetPhoto preset and adding AVCapturePhotoOutput, I cannot turn off image stabilization.

        
        self.capturePhotoOutput = AVCapturePhotoOutput.init()
        
        self.captureDevice = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera
                                                     , for: AVMediaType.video, position: .back)
        do {
            
            let input = try AVCaptureDeviceInput(device: self.captureDevice!)
            
            self.captureSession = AVCaptureSession()
            self.captureSession?.beginConfiguration()
            self.captureSession?.sessionPreset = .photo
            self.captureSession?.addInput(input)
            
            if ((captureSession?.canAddOutput(capturePhotoOutput!)) != nil) {
                captureSession?.addOutput(capturePhotoOutput!)
            }
            
            if let connection = capturePhotoOutput?.connection(with: .video) {
                if connection.isVideoStabilizationSupported {
                    connection.preferredVideoStabilizationMode = .off
                }
            }
             
            DispatchQueue.main.async { [self] in
                self.capturePhotoOutput?.isHighResolutionCaptureEnabled = true
                self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession!)
                self.videoPreviewLayer?.videoGravity = .resizeAspectFill
                
                self.videoPreviewLayer?.connection?.videoOrientation = .portrait
                
                self.videoPreviewLayer?.frame = self.previewView.layer.frame
                
                self.previewView.layer.insertSublayer(self.videoPreviewLayer!, at: 0)
                
            }
            
            self.captureSession?.commitConfiguration()
            self.captureSession?.startRunning()
        }
        
    }

    @objc private func handleTakePhoto(){
        
        let photoSettings = AVCapturePhotoSettings()
        if let photoPreviewType = photoSettings.availablePreviewPhotoPixelFormatTypes.first {
            photoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String:photoPreviewType]
            photoSettings.isAutoStillImageStabilizationEnabled = false
            capturePhotoOutput?.capturePhoto(with: photoSettings, delegate: self)
        }
    }
    
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        
        if let dataImage = photo.fileDataRepresentation() {
            print(UIImage(data: dataImage)?.size as Any)
            
            let dataProvider = CGDataProvider(data: dataImage as CFData)
            let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
            
            let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: rotateImage(orientation: currentOrientation))
            
        } else {
            print("some error here")
            
        }
    }

As a temporary solution, I added only AVCaptureVideoDataOutput to the session without adding AVCapturePhotoOutput, and I can capture in 4:3 format with the captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) function. However, this time I cannot get a 4K image.

In short, I need to turn off video stabilization in a session with AVCapturePhotoOutput added.

        
        
        self.captureDevice = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera
                                                     , for: AVMediaType.video, position: .back)
        do {
            
            let input = try AVCaptureDeviceInput(device: self.captureDevice!)
            
            self.captureSession = AVCaptureSession()
            self.captureSession?.beginConfiguration()
            self.captureSession?.sessionPreset = .photo
            self.captureSession?.addInput(input)
            
            videoDataOutput = AVCaptureVideoDataOutput()
            videoDataOutput?.videoSettings = [
                kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)
            ]
            videoDataOutput?.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
            
            
            if ((captureSession?.canAddOutput(videoDataOutput!)) != nil) {
                captureSession?.addOutput(videoDataOutput!)
            }
            
              
            /* If I cancel the comment line, video stabilization is enabled.
            if ((captureSession?.canAddOutput(capturePhotoOutput!)) != nil) {
                captureSession?.addOutput(capturePhotoOutput!)
            }
            */
            
            DispatchQueue.main.async { [self] in

                self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession!)
                self.videoPreviewLayer?.videoGravity = .resizeAspectFill
                
                self.videoPreviewLayer?.connection?.videoOrientation = .portrait
                
                self.videoPreviewLayer?.frame = self.previewView.layer.frame
                
                self.previewView.layer.insertSublayer(self.videoPreviewLayer!, at: 0)
                
            }
            
            self.captureSession?.commitConfiguration()
            self.captureSession?.startRunning()
        }
        
    }

    @objc private func handleTakePhoto(){
        takePicture = true
    }
    
    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        if !takePicture {
            return //we have nothing to do with the image buffer
        }
        
        //try and get a CVImageBuffer out of the sample buffer
        guard let cvBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
            return
        }
        
        
        
        
        let rect = CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(cvBuffer), height: CVPixelBufferGetHeight(cvBuffer))
        let ciImage = CIImage.init(cvImageBuffer: cvBuffer)
        let ciContext = CIContext()
        let cgImage = ciContext.createCGImage(ciImage, from: rect)
        guard cgImage != nil else {return }
        let uiImage = UIImage(cgImage: cgImage!)
    }
How do I disable video stabilization in a AVCaptureSession with AVCapturePhotoOutput added?
 
 
Q