【问题标题】:CGWindowListCreateImage yields blurred cgImage when zoomedCGWindowListCreateImage 缩放时产生模糊的 cgImage
【发布时间】:2017-02-19 19:21:17
【问题描述】:

我正在为 mac 开发类似放大镜的应用程序。我的目标是能够在放大时精确定位单个像素。我在 mouseMoved(带有事件: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 期间应用了一些抗锯齿。我的目标是获得每个像素的锐利表示。谁能指出我正确的方向?

【问题讨论】:

    标签: macos core-graphics retina-display nswindow zooming


    【解决方案1】:

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

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

    【讨论】:

    • 亲爱的@Tuslareb,请您提供一个context 的示例好吗?我面临同样的问题。我还使用CGWindowListCreateImage 生成屏幕截图,但结果 CGImage 在缩放时会变得模糊。我基本上将屏幕截图转换为NSImage,然后在 ImageView 中显示。
    • @ixany 我用一些示例代码更新了我的答案。基本上,我在自定义 NSView 的 drawRect 方法中绘制从 CGWindowListCreateImage 调用中获得的 CGImage。您在 CGWindowListCreateImage 调用中使用哪个 CGWindowImageOption?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多