isee15/Capture-Screen-For-Multi-Screens-On-Mac

关于计算 screen rect 的问题

ripperhe opened this issue · 3 comments

image

+ (CGImageRef)screenShot:(NSScreen *)screen
{
    CFArrayRef windowsRef = CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);

    NSRect rect = [screen frame];
    NSRect mainRect = [NSScreen mainScreen].frame;
    for (NSScreen *subScreen in [NSScreen screens]) {
        if ((int) subScreen.frame.origin.x == 0 && (int) subScreen.frame.origin.y == 0) {
            mainRect = subScreen.frame;
        }
    }
    rect = NSMakeRect(rect.origin.x, (mainRect.size.height) - (rect.origin.y + rect.size.height), rect.size.width, rect.size.height);

    NSLog(@"screenShot: %@", NSStringFromRect(rect));
    CGImageRef imgRef = CGWindowListCreateImageFromArray(rect, windowsRef, kCGWindowImageDefault);
    CFRelease(windowsRef);

    return imgRef;
}

我想问下为啥 y 是 (mainRect.size.height) - (rect.origin.y + rect.size.height)?
确实是要这样才是正确的,不过这是什么原理?

https://isaacpg001.github.io/programming/2011/08/05/mac-multi-display-screen-crop/
这个主要是CG的坐标原点和screen的坐标原点体系不一样

太感谢了,明白了 🤝

@isee15 我写了一个软件,截图部分参考的你的代码,特此感谢 🤝
https://github.com/ripperhe/Bob