Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Proper implementation of imageFromApplication on Mac

  • Loading branch information...
commit 3aba5259114c8652b13cb92bdbd67690244057b6 1 parent d0cd343
@MichaelBuckley MichaelBuckley authored
View
14 src/ImageCaptureRoute.m
@@ -108,13 +108,13 @@ - (HTTPFileResponse *)responseWithSnapshotOfViewWithUid:(NSString *)uid withConn
if ( [path count] > 1 && [@"view-snapshot" isEqualToString:[path objectAtIndex:1]] ){
return [self responseWithSnapshotOfViewWithUid:[path objectAtIndex:2] withConnection:connection];
}
-
- BOOL allWindows = [path count] > 1 && [[path objectAtIndex:1] isEqualToString:@"allwindows"];
#if TARGET_OS_IPHONE
+ BOOL allWindows = [path count] > 1 && [[path objectAtIndex:1] isEqualToString:@"allwindows"];
+
UIImage *screenshot = [UIImage imageFromApplication:allWindows];
#else
- NSImage *screenshot = [NSImage imageFromApplication:allWindows];
+ NSImage *screenshot = [NSImage imageFromApplication];
#endif
if ([path count] == 4)
@@ -142,7 +142,13 @@ - (HTTPFileResponse *)responseWithSnapshotOfViewWithUid:(NSString *)uid withConn
#if TARGET_OS_IPHONE
NSData *response = UIImagePNGRepresentation(screenshot);
#else
- NSData *response = [[[screenshot representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil];
+ [screenshot lockFocus];
+ NSSize size = [screenshot size];
+ NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect: NSMakeRect(0, 0, size.height, size.height)];
+ [screenshot unlockFocus];
+
+ NSData *response = [rep representationUsingType:NSPNGFileType properties:nil];
+ [rep release];
#endif
return [[[HTTPDataResponse alloc] initWithData:response] autorelease];
View
2  src/NSImage+Frank.h
@@ -10,7 +10,7 @@
@interface NSImage (Frank)
-+ (NSImage *)imageFromApplication:(BOOL)allWindows;
++ (NSImage *)imageFromApplication;
- (NSImage *)imageCropedToFrame:(CGRect)cropFrame;
- (NSImage *)imageMaskedAtFrame:(CGRect)maskFrame;
View
80 src/NSImage+Frank.m
@@ -9,24 +9,86 @@
#import "NSImage+Frank.h"
#import "NSView+FrankImageCapture.h"
+@interface NSApplication ()
+- (CGRect) FEX_accessibilityFrame;
+@end
+
@implementation NSImage (Frank)
-+ (NSImage *) imageFromApplication:(BOOL)allWindows
++ (NSImage *) imageFromApplication
{
- NSWindow* mainWindow = [[NSApplication sharedApplication] mainWindow];
+ NSRect screenFrame = (NSRect) [[NSApplication sharedApplication] FEX_accessibilityFrame];
+
+ NSImage* screenshot = [[[NSImage alloc] initWithSize: screenFrame.size] autorelease];
+
+ NSMutableDictionary* appWindows = [NSMutableDictionary dictionary];
+ NSMutableArray* screenshotWindows = [NSMutableArray array];
+
+ for (NSWindow* window in [[NSApplication sharedApplication] windows])
+ {
+ [appWindows setObject: window forKey: [NSNumber numberWithInteger: [window windowNumber]]];
+ }
- if (allWindows){
- NSMutableArray *views = [NSMutableArray array];
+ NSArray* allWindows = (NSArray*) CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
+
+ for (NSDictionary* windowInfo in allWindows)
+ {
+ NSNumber* windowNumber = [windowInfo objectForKey: (NSString*) kCGWindowNumber];
- for (NSWindow *window in [[NSApplication sharedApplication] windows]){
- [views addObject:[window contentView]];
+ if ([appWindows objectForKey: windowNumber] != nil)
+ {
+ [screenshotWindows addObject: [appWindows objectForKey: windowNumber]];
}
+ }
+
+ [allWindows release];
+
+ [screenshot lockFocus];
+
+ for (NSScreen* screen in [NSScreen screens])
+ {
+ NSRect frame = [screen frame];
- return [NSView captureImageOfSize:mainWindow.frame.size fromViews:views];
+ NSURL* backgroundURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen: screen];
+ NSData* data = [NSData dataWithContentsOfURL: backgroundURL];
+ NSImage* background = [[NSImage alloc] initWithData: data];
+
+ [screenshot lockFocus];
+
+ [background drawInRect: frame
+ fromRect: NSZeroRect
+ operation: NSCompositeSourceOver
+ fraction: 1.0];
+
+ [screenshot unlockFocus];
+
+ [background release];
}
- else{
- return [[mainWindow contentView] captureImage];
+
+ for (NSWindow* window in [screenshotWindows reverseObjectEnumerator])
+ {
+ NSInteger windowNumber = [window windowNumber];
+ CGImageRef cgImage = CGWindowListCreateImage(CGRectNull,
+ kCGWindowListOptionIncludingWindow,
+ (CGWindowID) windowNumber,
+ kCGWindowImageDefault);
+
+ NSImage* nsImage = [[NSImage alloc] initWithCGImage: cgImage size: [window frame].size];
+
+ [screenshot lockFocus];
+
+ [nsImage drawInRect: [window frame]
+ fromRect: NSZeroRect
+ operation: NSCompositeSourceOver
+ fraction: 1.0];
+
+ [screenshot unlockFocus];
+
+ [nsImage release];
+ CFRelease(cgImage);
}
+
+ return screenshot;
}
- (NSImage *)imageCropedToFrame:(CGRect)cropFrame
Please sign in to comment.
Something went wrong with that request. Please try again.