Alec
Alec

Reputation: 464

Pixel picking in Swift

I am trying to create a functionality (for a macOS 11+ application) where I pick a pixel from the screen (e.g like the one from Digital color pickier/ any color picker). However, I am having a hard time finding a way to do that.

I tried to "reverse engineer" some applications from AppStore and when I want to pick the pixel, the apps ask me if I want to give permission to record the screen, so I guess that they are "recording the screen" and they exact the pixel from a movie/picture that they record on the spot. However, this solutions does seems a little bit overkill and I think there should be a better way (a good example is Digital Colour Meter that does not ask for permissions).

Do you think that this can be achievable in a somewhat easy and clear manner? Also, if not, is my guess right with "recording the screen" and capturing a pixel from within the clip (of course, in real time)?

Upvotes: 2

Views: 147

Answers (1)

Alexander
Alexander

Reputation: 63369

NSColorSampler was introduced in macOS 10.15 (Catalina). You can use it to sample a color from the screen using the system's built-in color picking interface. It does not require screen-recording permissions.

It's mentioned around the 5 minutes mark in WWDC 2019, Session 210, What’s New in AppKit for macOS

I would probably wrap it, and take a hybrid approach where I use old methods (with the screen permission prompt) on old versions, and NSColorSampler for new versions.

Upvotes: 3

Related Questions