图形上下文是预期的两倍大



我正在开发一款Swift MacOS绘图应用程序。下面的代码子集显示了我的问题。

import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
var window: NSWindow!
window = NSWindow(contentRect: NSMakeRect( 0, 0, 1000, 1000 ),styleMask:[.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
window.makeKeyAndOrderFront(window)
print("windowHeight = (window.frame.height)")
print("windowWidth  = (window.frame.width)")

let view = Renderer( frame:NSMakeRect( 0, 0, 1000, 1000 ) )
view.autoresizingMask = [.width, .height]
window.contentView!.addSubview (view)
print("viewHeight = (view.frame.height)")
print("viewWidth  = (view.frame.width)")
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool{return true}
func applicationWillTerminate(_ aNotification: Notification) { }
}
let appDelegate = AppDelegate()
let application = NSApplication.shared
application.setActivationPolicy(.regular)
application.delegate = appDelegate
application.activate(ignoringOtherApps:true)
application.run()

class Renderer: NSView {

override func draw(_ rect: NSRect) {
super.draw(rect)

print("rectHeight = (rect.height)")
print("rectWidth  = (rect.width)")

guard let gc = NSGraphicsContext.current?.cgContext else {return}  // gc = graphics context

print("gc.height = (gc.height)")
print("gc.width  = (gc.width)")

// The rest of my drawing code goes here.
}
}

运行此代码时,打印输出为:

windowHeight = 1022.0   windowWidth = 1000.0
viewHeight   = 1000.0   viewWidth   = 1000.0
rectHeight   = 1000.0   rectWidth   = 1000.0
gc.height    = 2000     gc.width    = 2000

当我已经声明window、view和rect的大小都是1000乘1000时,为什么当前GraphicsContext说它的大小是2000乘2000?

因为您的计算机有一个双倍分辨率(Retina(屏幕。

相关内容

  • 没有找到相关文章

最新更新