gpt4 book ai didi

objective-c - 在 objective-c cocoa 应用程序中获取屏幕上像素的颜色

转载 作者:太空狗 更新时间:2023-10-30 03:47:54 26 4
gpt4 key购买 nike

我正在尝试在 objective-c 中创建一个颜色选择器 cocoa 应用程序,这样我就可以获得屏幕上任何像素的颜色。有没有办法在 objective-c 中获取某个屏幕像素的颜色?

最佳答案

您可以使用 CGDisplayCreateImageForRect 获取包含您感兴趣的点的 CGImageRef。一旦您拥有 CGImage,您可以通过将图像渲染到自定义缓冲区或使用 NSBitmapImageRep 的 initWithCGImage 和然后是 colorAtX:y:。 NSBitmapImageRep 方法的“在堆栈溢出代码窗口中编写”代码看起来像这样:

NSColor *MyColorAtScreenCoordinate(CGDirectDisplayID displayID, NSInteger x, NSInteger y) {
CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(x, y, 1, 1));
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
CGImageRelease(image);
NSColor *color = [bitmap colorAtX:0 y:0];
[bitmap release];
}

这可能会返回设备颜色空间中的颜色。返回校准后的 RBG 颜色空间中的颜色可能会很有用。

关于objective-c - 在 objective-c cocoa 应用程序中获取屏幕上像素的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4395420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com