Given an NSView, how can I get its screen-rect and the display it's on, so I can use CGDisplayCreateImageForRect?

I found the NSWindow convertRectToScreen method, but it's only available from 10.7 (I need 10.6 as well), and it still doesn't give the display the NSView is on.

有帮助吗?

解决方案

Assuming you don't care about getting the actual pixel size of the view (because it'd differ between HDPI and regular displays -- I can't imagine it'd be a useful value right now), you basically just want to use a combination of NSView's convertRect:toView: and NSWindow's convertBaseToScreen:. So, for example, [someView.window convertBaseToScreen:[someView convertRect:someView.bounds toView:nil]]. This is assuming you aren't transforming the view in any unusual-ish sort of ways (rotating it or something). Bear in mind that you should still add a check for whether the window responds to convertRectToScreen: and use that if it does -- that way you avoid using the deprecated method (convertBaseToScreen:) where possible.

Afterward, if you need the exact pixel size for the screen, you'd use NSScreen's convertRectToBacking: (I'm not sure if you'll need it for this). As for determining the screen, NSWindow has its screen method, which should be sufficient in most cases. The only issue I can see is when the view crosses screens, so you may have to check if it's intersecting any other displays and merge images and so on. That said, I'm not sure if CGDisplayCreateImageForRect will account for this or not. You'll have to test that yourself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top