跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

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

发表于

代码如下:

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!")
                    }
                }
            }
        }
        
    }
    
}

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.