Objective c 如何使用Cocoa和CGDisplayCreateImageForRect在Mac OS X中拍摄区域屏幕截图?
如何使用Cocoa和CGDisplayCreateImageForRect在Mac OS X中拍摄区域屏幕截图?我找到了如何制作全尺寸屏幕截图,但我找到了如何制作区域屏幕截图?对不起,格式太难看了。第二个函数正是您想要的Objective c 如何使用Cocoa和CGDisplayCreateImageForRect在Mac OS X中拍摄区域屏幕截图?,objective-c,macos,Objective C,Macos,如何使用Cocoa和CGDisplayCreateImageForRect在Mac OS X中拍摄区域屏幕截图?我找到了如何制作全尺寸屏幕截图,但我找到了如何制作区域屏幕截图?对不起,格式太难看了。第二个函数正是您想要的 // this is a cute function for creating CGImageRef from NSImage. // I found it somewhere on SO but I do not remember the link, I am sorry
// this is a cute function for creating CGImageRef from NSImage.
// I found it somewhere on SO but I do not remember the link, I am sorry..
CGImageRef CGImageCreateWithNSImage(NSImage *image) {
NSSize imageSize = [image size];
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, [[NSColorSpace genericRGBColorSpace] CGColorSpace], kCGBitmapByteOrder32Host|kCGImageAlphaPremultipliedFirst);
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:bitmapContext flipped:NO]];
[image drawInRect:NSMakeRect(0, 0, imageSize.width, imageSize.height) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);
return cgImage;
}
NSImage* NSImageFromScreenWithRect(CGRect rect){
// copy screenshot to clipboard, works on OS X only..
system("screencapture -c -x");
// get NSImage from clipboard..
NSImage *imageFromClipboard=[[NSImage alloc]initWithPasteboard:[NSPasteboard generalPasteboard]];
// get CGImageRef from NSImage for further cutting..
CGImageRef screenShotImage=CGImageCreateWithNSImage(imageFromClipboard);
// cut desired subimage from fullscreen screenshot..
CGImageRef screenShotCenter=CGImageCreateWithImageInRect(screenShotImage,rect);
// create NSImage from CGImageRef..
NSImage *resultImage=[[NSImage alloc]initWithCGImage:screenShotCenter size:rect.size];
// release CGImageRefs cause ARC has no effect on them..
CGImageRelease(screenShotCenter);
CGImageRelease(screenShotImage);
return resultImage;
}
从阅读开始,然后问一个更具体的问题…这是我知道的。如何获取CGDisplayCreateImageForRect的鼠标坐标?