2017-02-19 30 views
2

我正在开发一个类似mac的应用程序的放大镜。我的目标是能在放大时查明单个像素我正在使用的mouseMoved此代码(与事件:NSEvent):CGWindowListCreateImage在放大时会产生模糊的cgImage

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的过程中,施加一定的反走样。我的目标是获得每个像素的锐利表示。任何人都可以将我指向正确的方向吗?

回答

2

好吧,我想通了。这是设置的插值质量没有在绘图方面的问题:

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)) 
+0

亲爱@Tuslareb,你能否如此善良,并提供一个“背景”的例子?我面临同样的问题。我也使用'CGWindowListCreateImage'生成截图,但缩放时产生的CGImage会模糊。我基本上将屏幕截图转换为“NSImage”,然后将其显示在ImageView中。 – ixany

+0

@ixany我用一些示例代码更新了我的答案。基本上,我在CGWindowListCreateImage调用中绘制了自定义NSView的drawRect方法中的CGImage。你在CGWindowListCreateImage调用中使用了哪个CGWindowImageOption? – Tuslareb