CGWindowListCreateImage 在缩放时产生模糊的 cgImage
CGWindowListCreateImage yields blurred cgImage when zoomed
我正在为 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 中绘制它时,结果如下所示:
它看起来很模糊/好像在创建 cgImage 的过程中应用了一些抗锯齿。我的目标是获得每个像素的锐利表示。谁能指出我正确的方向?
好的,我明白了。这是在绘图上下文中将插值质量设置为 none 的问题:
context.interpolationQuality = .none
结果:
请求更多代码:
//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))
我正在为 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 中绘制它时,结果如下所示:
它看起来很模糊/好像在创建 cgImage 的过程中应用了一些抗锯齿。我的目标是获得每个像素的锐利表示。谁能指出我正确的方向?
好的,我明白了。这是在绘图上下文中将插值质量设置为 none 的问题:
context.interpolationQuality = .none
结果:
请求更多代码:
//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))