Skip to content

Commit

Permalink
Proper implementation of imageFromApplication on Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBuckley committed Feb 24, 2013
1 parent d0cd343 commit 3aba525
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/ImageCaptureRoute.m
Expand Up @@ -108,13 +108,13 @@ - (HTTPFileResponse *)responseWithSnapshotOfViewWithUid:(NSString *)uid withConn
if ( [path count] > 1 && [@"view-snapshot" isEqualToString:[path objectAtIndex:1]] ){ if ( [path count] > 1 && [@"view-snapshot" isEqualToString:[path objectAtIndex:1]] ){
return [self responseWithSnapshotOfViewWithUid:[path objectAtIndex:2] withConnection:connection]; return [self responseWithSnapshotOfViewWithUid:[path objectAtIndex:2] withConnection:connection];
} }

BOOL allWindows = [path count] > 1 && [[path objectAtIndex:1] isEqualToString:@"allwindows"];


#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
BOOL allWindows = [path count] > 1 && [[path objectAtIndex:1] isEqualToString:@"allwindows"];

UIImage *screenshot = [UIImage imageFromApplication:allWindows]; UIImage *screenshot = [UIImage imageFromApplication:allWindows];
#else #else
NSImage *screenshot = [NSImage imageFromApplication:allWindows]; NSImage *screenshot = [NSImage imageFromApplication];
#endif #endif


if ([path count] == 4) if ([path count] == 4)
Expand Down Expand Up @@ -142,7 +142,13 @@ - (HTTPFileResponse *)responseWithSnapshotOfViewWithUid:(NSString *)uid withConn
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
NSData *response = UIImagePNGRepresentation(screenshot); NSData *response = UIImagePNGRepresentation(screenshot);
#else #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 #endif


return [[[HTTPDataResponse alloc] initWithData:response] autorelease]; return [[[HTTPDataResponse alloc] initWithData:response] autorelease];
Expand Down
2 changes: 1 addition & 1 deletion src/NSImage+Frank.h
Expand Up @@ -10,7 +10,7 @@


@interface NSImage (Frank) @interface NSImage (Frank)


+ (NSImage *)imageFromApplication:(BOOL)allWindows; + (NSImage *)imageFromApplication;
- (NSImage *)imageCropedToFrame:(CGRect)cropFrame; - (NSImage *)imageCropedToFrame:(CGRect)cropFrame;
- (NSImage *)imageMaskedAtFrame:(CGRect)maskFrame; - (NSImage *)imageMaskedAtFrame:(CGRect)maskFrame;


Expand Down
80 changes: 71 additions & 9 deletions src/NSImage+Frank.m
Expand Up @@ -9,24 +9,86 @@
#import "NSImage+Frank.h" #import "NSImage+Frank.h"
#import "NSView+FrankImageCapture.h" #import "NSView+FrankImageCapture.h"


@interface NSApplication ()
- (CGRect) FEX_accessibilityFrame;
@end

@implementation NSImage (Frank) @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){ NSArray* allWindows = (NSArray*) CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
NSMutableArray *views = [NSMutableArray array];
for (NSDictionary* windowInfo in allWindows)
{
NSNumber* windowNumber = [windowInfo objectForKey: (NSString*) kCGWindowNumber];


for (NSWindow *window in [[NSApplication sharedApplication] windows]){ if ([appWindows objectForKey: windowNumber] != nil)
[views addObject:[window contentView]]; {
[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 - (NSImage *)imageCropedToFrame:(CGRect)cropFrame
Expand Down

0 comments on commit 3aba525

Please sign in to comment.