在我的可可应用程序中,我想为多屏幕拍摄桌面墙纸的屏幕快照,我发现此link是我为单屏幕制作的,但是,我发现很难在多屏幕上使用。那么我将如何在多个屏幕上做到这一点?
这是我的代码
- (NSImage*) desktopAsImageWithScreen:(NSScreen *)screen {
NSDictionary *deviceDescription = [screen deviceDescription];
CFMutableArrayRef windowIDs = CFArrayCreateMutable(NULL, 0, NULL);
CFArrayRef allWindowIDs = CGWindowListCreate(kCGWindowListOptionOnScreenBelowWindow, kCGDesktopIconWindowLevel);
NSLog(@"min window level: %d", kCGMinimumWindowLevel);
NSLog( @"looking for windows on level: %d", kCGDesktopWindowLevel );
NSLog( @"NOt actually looking for windows on level: %d", kCGMinimumWindowLevel );
NSLog( @"NOt actually looking for windows on the dock level: %d", kCGDockWindowLevel );
if (allWindowIDs)
{
CFArrayRef windowDescs = CGWindowListCreateDescriptionFromArray(allWindowIDs);
for (CFIndex idx=0; idx<CFArrayGetCount(windowDescs); idx++)
{
CFDictionaryRef dict = CFArrayGetValueAtIndex(windowDescs, idx);
CFStringRef ownerName = CFDictionaryGetValue(dict, kCGWindowOwnerName);
NSLog(@"owner name = %@", ownerName);
if (CFStringCompare(ownerName, CFSTR("Dock"), 0) == kCFCompareEqualTo )
{
// the Dock level has the dock and the desktop picture
CGRect windowBounds;
BOOL success = CGRectMakeWithDictionaryRepresentation(
(CFDictionaryRef)(CFDictionaryGetValue(dict, kCGWindowBounds)),
&windowBounds);
NSRect screenBounds = screen.frame;
CFNumberRef windowLayer = CFDictionaryGetValue(dict, kCGWindowLayer);
//CFDictionaryGetValue(dict, kCGWindowBounds)
NSLog(@"window bounds %f, %f, matches screen bounds? %d, on level %@",
windowBounds.size.width,
windowBounds.size.height,
CGRectEqualToRect(windowBounds, screenBounds),
windowLayer
);
NSNumber* ourDesiredLevelNumber = [NSNumber numberWithInt: kCGDesktopWindowLevel - 1];
if ( CGRectEqualToRect(windowBounds, screenBounds) && [ourDesiredLevelNumber isEqualToNumber: (__bridge NSNumber * _Nonnull)(windowLayer)] )
CFArrayAppendValue(windowIDs, CFArrayGetValueAtIndex(allWindowIDs, idx));
}
}
CFRelease(windowDescs);
CFRelease(allWindowIDs);
}
CGImageRef cgImage = CGWindowListCreateImageFromArray( screen.frame, windowIDs, kCGWindowImageDefault);
// Create a bitmap rep from the image...
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
// Create an NSImage and add the bitmap rep to it...
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
//[bitmapRep release];
return image;
}
有什么建议吗?
预先感谢!