ScreenCaptureKit 里面的 SCStreamOutput 没有被顺利触发,是那个步骤错了,求解惑 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
happydoze
V2EX    Swift

ScreenCaptureKit 里面的 SCStreamOutput 没有被顺利触发,是那个步骤错了,求解惑

  •  1
     
  •   happydoze 2024-09-20 11:33:03 +08:00 2523 次点击
    这是一个创建于 384 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下:

    import SwiftUI import ScreenCaptureKit struct ContentView: View { @State private var showingScreenshotAlert = false @State private var screenshotRect = CGRect(x: 100, y: 100, width: 400, height: 300) // 示例区域 @State private var cc = "截图 1" var body: some View { VStack { Text("点击下方按钮进行截图") .padding() Button(action: takeScreenshot) { Text(cc) .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(8) } } .alert(isPresented: $showingScreenshotAlert) { Alert(title: Text("截图完成"), message: Text("截图已保存至桌面"), dismissButton: .default(Text("确定"))) } } class MyStreamOutput: NSObject, SCStreamOutput { var onCaptureCompletion: (() -> Void)? // 闭包,用于通知截图完成 func stream(_ stream: SCStream, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, of type: SCStreamOutputType) { // 每当捕获到新的样本帧时打印消息 print("Captured a new frame of type") onCaptureCompletion?()// 调用闭包通知截图完成 } } func takeScreenshot() { cc = "在截图 1..." showingScreenshotAlert = true SCShareableContent.getWithCompletionHandler { (content, error) in if let error = error { print("Error: \(error)") } else if let cOntent= content { print("Displays: \(content.displays)") print("Displays: \(content.displays[0])") print("Displays: \(content.displays[0].width)") print("Displays: \(content.displays[0].height)") print("Windows:\(content.windows[23])") print("Windows ID:\(content.windows[23].windowID)") print("Windows Count:\(content.windows.count)") // MARK: - 创建 // 创建一个过滤器以捕获整个屏幕 let filter1 = SCContentFilter(display: content.displays[0], excludingWindows: []) let config1 = SCStreamConfiguration() let stream1 = SCStream(filter: filter1, configuration: config1, delegate: nil) // 实例化我们的输出处理类 let streamOutput = MyStreamOutput() // 定义截图完成时的动作 streamOutput.OnCaptureCompletion= { DispatchQueue.main.async { cc = "截图完成了" print("-----------") } } // 添加输出 do { try stream1.addStreamOutput(streamOutput, type: .screen, sampleHandlerQueue: DispatchQueue.main) print("流输出已成功添加") } catch { print("添加流输出时出错: \(error)") } stream1.startCapture { error in if let error = error { print("Failed to start capture: \(error)") } else { print("Capture started successfully!") } } } } } } 
    happydoze
        1
    happydoze  
    OP
       2024-09-22 14:55:11 +08:00
    那自己回答吧,不知道为什么,只有异步才成功。不知道是不是线程的问题。有知道答案的人分析一下么
    贴一下成功的代码

    ```swift

    func signIn4() {
    Task {
    do {
    let cOntent= try await getShareableContent()
    guard let display = content.displays.first else { return }
    print(display)
    let filter = SCContentFilter(display: display, excludingWindows: [])
    let cOnfig= SCStreamConfiguration()
    config.minimumFrameInterval = CMTime(value: 1, timescale: 10)
    let stream = SCStream(filter: filter, configuration: config, delegate: nil)
    let output = Output()
    try stream.addStreamOutput(output, type: .screen, sampleHandlerQueue: nil)
    try await stream.startCapture()
    try await Task.sleep(nanoseconds: 200_000_000)
    try await stream.stopCapture()
    } catch {
    print("Failed to retrieve capturable content or start stream: \(error.localizedDescription)")
    }
    }
    }

    func getShareableContent() async throws -> SCShareableContent {
    return try await withCheckedThrowingContinuation { continuation in
    SCShareableContent.getWithCompletionHandler { content, error in
    if let error = error {
    continuation.resume(throwing: error)
    } else if let cOntent= content {
    continuation.resume(returning: content)
    }
    }
    }
    }

    ```
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2812 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 13:36 PVG 21:36 LAX 06:36 JFK 09:36
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86