我正在尝试获取整个显示的屏幕截图。然而,我返回的是不同的分辨率,这也是非视网膜的。而不是
3456x2234
CGImageRef
我得到的是 1920x1080
。
请注意,旧 API
CGWindowListCreateImage
已在 macOS 15.0 中过时,并会触发无限隐私弹出警告。旧 API 给出了预期结果。
CG_EXTERN CGImageRef __nullable CGWindowListCreateImage(CGRect screenBounds,
CGWindowListOption listOption, CGWindowID windowID,
CGWindowImageOption imageOption)
SCREEN_CAPTURE_OBSOLETE(10.5,14.0,15.0);
代码:
[SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent * _Nullable shareableContent, NSError * _Nullable error) {
if (error) { return; }
SCDisplay *display = [[shareableContent displays] firstObject];
SCContentFilter *filter = [[SCContentFilter alloc] initWithDisplay:display excludingWindows:@[]];
SCStreamConfiguration *configuration = [[SCStreamConfiguration alloc] init];
configuration.capturesAudio = NO;
configuration.excludesCurrentProcessAudio = YES;
configuration.preservesAspectRatio = YES;
configuration.showsCursor = NO;
configuration.captureResolution = SCCaptureResolutionBest;
//configuration.pixelFormat = 'BGRA';
[SCScreenshotManager captureImageWithFilter:filter configuration:configuration completionHandler:^(CGImageRef _Nullable cgImage, NSError * _Nullable error) {
if (error) {
} else {
NSLog(@"%@", cgImage);
}
}];
}];
日志:
Printing description of cgImage:
<CGImage 0x11fe306c0> (DP)
<<CGColorSpace 0x600002ba0a80> (kCGColorSpaceDeviceRGB)>
width = 1920, height = 1080, bpc = 8, bpp = 32, row bytes = 7680
kCGImageAlphaPremultipliedFirst | kCGImageByteOrder32Little | kCGImagePixelFormatPacked
is mask? No, has masking color? No, has soft mask? No, has matte? No, should interpolate? Yes
(lldb) po [NSScreen mainScreen]
<NSScreen: 0x600002bac3c0; name="Built-in Retina Display"; backingScaleFactor=2.000000; frame={{0, 0}, {1728, 1117}}; visibleFrame={{0, 48}, {1728, 1031}}>
旧 API 将返回:
Printing description of cgImage:
<CGImage 0x146e8ad90> (DP)
<<CGColorSpace 0x600000c949c0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; Color LCD)>
width = 3456, height = 2234, bpc = 8, bpp = 32, row bytes = 13824
kCGImageAlphaPremultipliedFirst | kCGImageByteOrder32Little | kCGImagePixelFormatPacked
is mask? No, has masking color? No, has soft mask? No, has matte? No, should interpolate? No
SCStreamConfiguration
默认为 1920x1080
。必须手动配置。
NSRect screenFrame = [window screen] ? [[window screen] frame] : NSMakeRect(0, 0, 1920, 1080);
CGFloat backingScaleFactor = [window screen] ? [[window screen] backingScaleFactor] : 1.0;
[SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent * _Nullable shareableContent, NSError * _Nullable error) {
if (error) { return; }
SCDisplay *display = [[shareableContent displays] firstObject];
SCContentFilter *filter = [[SCContentFilter alloc] initWithDisplay:display excludingWindows:@[]];
SCStreamConfiguration *configuration = [[SCStreamConfiguration alloc] init];
configuration.capturesAudio = NO;
configuration.excludesCurrentProcessAudio = YES;
configuration.preservesAspectRatio = YES;
configuration.showsCursor = NO;
configuration.captureResolution = SCCaptureResolutionBest;
configuration.width = screenFrame.size.width * backingScaleFactor;
configuration.height = screenFrame.size.width * backingScaleFactor;
//configuration.pixelFormat = 'BGRA';
[SCScreenshotManager captureImageWithFilter:filter configuration:configuration completionHandler:^(CGImageRef _Nullable cgImage, NSError * _Nullable error) {
if (error) {
} else {
NSLog(@"%@", cgImage);
}
}];
}];