Home > Software engineering >  Show full camera width from Mac camera when running app via Mac Catalyst
Show full camera width from Mac camera when running app via Mac Catalyst

Time:11-23

I know my Macbook's webcam has a wide field of view. When I open the Quicktime app and select my MacBooks camera as the input I see a nice wide video feed like this.

enter image description here

Now, I have an iOS app which I'm running using Mac Catalyst. Here is the build combination I am using.

enter image description here

When I run this app I get a significantly cropped version of the video feed from the web cam.

enter image description here

Here is the class where I set up the AVCaptureSession

import Foundation
import AVFoundation


class BasicCamera: ObservableObject {
    
    @Published var authorizationState: AVAuthorizationStatus = .notDetermined
    
    let session = AVCaptureSession()
    
    init() {
        guard let device = AVCaptureDevice.default(for: .video) else {
            fatalError("Could not make capture device.")
        }
        
        guard let input = try? AVCaptureDeviceInput(device: device) else {
            fatalError("Could not make input")
        }
        
        session.beginConfiguration()
        
        session.addInput(input)
        
        session.commitConfiguration()
        
        session.startRunning()
    }
    
    public func requestCameraPermission() async -> AVAuthorizationStatus {
        return await withCheckedContinuation({ continuation in
            AVCaptureDevice.requestAccess(for: .video) { [unowned self] didComplete in
                self.authorizationState = AVCaptureDevice.authorizationStatus(for: .video)
                continuation.resume(with: .success(self.authorizationState))
            }
        })
    }
    
}

And add it to the ViewController like this:

let preview = AVCaptureVideoPreviewLayer(session: session)
preview.removeFromSuperlayer()  
preview.frame = self.view.bounds      
self.view.layer.insertSublayer(preview, at: 0)

Note that the UIViewController I'm using is brought into SwiftUI via UIViewControllerRepresentable protocol conformance.

I have tried a number of presets and discovery sessions parameters including .buildInWidtAngleCamera but can't seem to get it to show the full camera resolution? Am I out of luck here unless I build it as an actual native Mac Application?

CodePudding user response:

Switch to Optimize interface for Mac it is under Target>General>Deployment Info>Next To the Mac checkmark button.

It is likely identifying the camera as portrait for the iPad

  • Related