Mac平台基于AVFoundation录屏分辨率过低的修改方法求助
Hey there! I’ve dealt with this exact low-resolution headache when building macOS screen recording tools using Apple’s official docs, so I know how frustrating it is to get stuck with that 480x300 default. Let’s break down how to adjust the resolution to what you need.
Key Issue to Address
The sample code in QA1740 uses default capture settings that prioritize low resource usage over resolution. To fix this, you need to explicitly override these defaults with your desired dimensions.
Step-by-Step Fix
1. Define Your Target Resolution
Decide if you want to record the full native screen resolution or a custom size. For full screen, grab the main screen’s bounds directly:let screenBounds = NSScreen.main?.frame ?? CGRect(x: 0, y: 0, width: 1920, height: 1080)For a custom resolution (like 1080p), define it explicitly:
let desiredResolution = CGSize(width: 1920, height: 1080) let captureRect = CGRect(origin: .zero, size: desiredResolution)2. Configure the AVCaptureScreenInput
When setting up yourAVCaptureScreenInput, override the default crop rect and frame rate to match your resolution:guard let screenInput = AVCaptureScreenInput(displayID: CGMainDisplayID()) else { fatalError("Failed to create screen capture input") } // Set the capture area to your desired resolution screenInput.cropRect = screenBounds // or captureRect for custom size // Set your preferred frame rate (e.g., 30fps) screenInput.minFrameDuration = CMTimeMake(value: 1, timescale: 30)3. Set the AVCaptureSession Preset
The session preset can restrict resolution, so use a preset that supports your target size. For maximum flexibility, use.inputPriorityto prioritize your input’s settings over the preset:let captureSession = AVCaptureSession() captureSession.sessionPreset = .inputPriority // Ensures your input's resolution is used if captureSession.canAddInput(screenInput) { captureSession.addInput(screenInput) }Alternatively, if you know your exact target resolution, you can use a specific preset like:
captureSession.sessionPreset = AVCaptureSession.Preset(rawValue: "AVCaptureSessionPreset1920x1080")
Why This Works
The QA1740 sample uses low-resolution defaults to keep the demo lightweight. By explicitly setting the cropRect on the screen input and using the right session preset, you override those defaults and force the capture to use your desired resolution.
内容的提问来源于stack exchange,提问作者Nikhil Nilawar

