You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 03:27:20