user2132980
user2132980

Reputation: 225

ScreenCaptureKit capture image with multiple displays

I have a macOS app that uses screen capture logic. It was originally coded using the Quartz CG API:

if let cgimage = CGDisplayCreateImage(CGMainDisplayID(), rect: cgRect) {...}

and this worked as expected even when capturing a screen rect that began on secondary monitor and ended on primary monitor (or was entirely contained on secondary monitor). However now that API is deprecated and you're supposed to use ScreenCaptureKit instead.
So I have attempted to convert the code. The trial code is:

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 {
           print("image dims \(cgim.width), \(cgim.height), requested: \(drect)")
                self.writeToFile2(cgim)
                ... //process the image
            }
            else {
                print("SCREEN CAP failed")
            }
        }
     ...

where sFilter was previously set based on main screen display (with no exclusions). This code also "works" as long as the capture rect is entirely on primary monitor. But it fails if the rect spans both monitors or is fully contained on secondary monitor. By "fails" I mean it produces empty image for the portion of the rect on secondary monitor.
So my question is: How to use ScreenCaptureKit to obtain screen shot of rectangle that spans dual monitors?

Upvotes: 3

Views: 205

Answers (0)

Related Questions