Created
November 25, 2013 03:29
-
-
Save TomLiu/7635876 to your computer and use it in GitHub Desktop.
Get main screen shot as NSImage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSImage+ScreenShot.h | |
// YunPan for Mac | |
// | |
// Created by 61 on 13-11-25. | |
// | |
// | |
#import <Cocoa/Cocoa.h> | |
@interface NSImage (ScreenShot) | |
+ (NSImage *)mainScreenShot; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSImage+ScreenShot.m | |
// YunPan for Mac | |
// | |
// Created by 61 on 13-11-25. | |
// | |
// | |
#import "NSImage+ScreenShot.h" | |
@implementation NSImage (ScreenShot) | |
+ (NSImage *)mainScreenShot | |
{ | |
NSRect screenRect = [[NSScreen mainScreen] frame]; | |
CGImageRef cgImage = CGWindowListCreateImage(screenRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault); | |
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; | |
CGImageRelease(cgImage); | |
NSImage *image = [[NSImage alloc] init]; | |
[image addRepresentation:rep]; | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment