gpt4 book ai didi

macos - CGWindowListCreateImage 在缩放时产生模糊的 cgImage

转载 作者:行者123 更新时间:2023-12-04 12:49:18 27 4
gpt4 key购买 nike

我正在为 mac 开发一个类似放大镜的应用程序。我的目标是能够在放大时精确定位单个像素。我在 mouseMoved(with event: NSEvent) 中使用此代码:

let captureSize = self.frame.size.width / 9     //9 is the scale factor
let screenFrame = (NSScreen.main()?.frame)!
let x = floor(point.x) - floor(captureSize / 2)
let y = screenFrame.size.height - floor(point.y) - floor(captureSize / 2)

let windowID = CGWindowID(self.windowNumber)

cgImageExample = CGWindowListCreateImage(CGRect(x: x, y: y, width: captureSize,
height: captureSize), CGWindowListOption.optionOnScreenBelowWindow, windowID,
CGWindowImageOption.bestResolution)

cgImage 的创建发生在 CGWindowListCreateImage 方法中。当我稍后在 NSView 中绘制它时,结果如下所示:

enter image description here

它看起来很模糊/就像在创建 cgImage 期间应用了一些抗锯齿。我的目标是获得每个像素的锐利表示。任何人都可以指出我正确的方向吗?

最佳答案

好的,我想通了。这是在绘图上下文中将插值质量设置为无的问题:

 context.interpolationQuality = .none

结果:

enter image description here

应要求提供更多代码:
//get the context
guard let context = NSGraphicsContext.current()?.cgContext else { return }

//get the CGImage
let image: CGImage = //pass the result from CGWindowListCreateImage call

//draw
context.draw(image, in: (CGRect of choice))

关于macos - CGWindowListCreateImage 在缩放时产生模糊的 cgImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42331735/

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