如何使用ScreenCaptureKit实现跨多显示器的屏幕矩形捕获?
解决ScreenCaptureKit跨双显示器截图空白问题
你的问题核心是内容过滤器(Content Filter)仅包含主显示器,导致副屏区域不在捕获范围内,最终生成空白。以下是修正方案:
关键调整点
- 必须创建包含所有目标显示器的
SCContentFilter,而非仅主显示器 - 确保
sourceRect使用系统全局屏幕坐标系(和原CG API的坐标逻辑一致)
完整修正代码
do { // 获取当前可捕获的所有显示器 let shareableContent = try SCShareableContent.current let allDisplays = shareableContent.displays // 创建包含所有显示器的内容过滤器,不排除任何应用/窗口 let sFilter = SCContentFilter(displays: allDisplays, excludingApplications: [], exceptingWindows: []) let scConfig = SCStreamConfiguration() scConfig.sourceRect = drect // 保持原全局坐标系下的目标矩形 scConfig.width = Int(drect.width) scConfig.height = Int(drect.height) SCScreenshotManager.captureImage(contentFilter: sFilter, configuration: scConfig) { any, error in if let cgim = any as? CGImage { print("image dims \(cgim.width), \(cgim.height), requested: \(drect)") self.writeToFile2(cgim) // 后续图像处理逻辑 } else { print("SCREEN CAP failed: \(error?.localizedDescription ?? "Unknown error")") } } } catch { print("Failed to access shareable content: \(error.localizedDescription)") }
额外注意事项
- 确认应用已获得屏幕录制权限:前往系统偏好设置 → 安全性与隐私 → 屏幕录制,勾选你的应用
- 如果仅需要捕获特定几个显示器(而非全部),可以先判断目标矩形
drect覆盖了哪些显示器,再将这些显示器传入SCContentFilter(displays:),避免不必要的资源占用
内容的提问来源于stack exchange,提问作者user2132980
相关产品推荐
相关产品推荐

