在OSX上用C++捕获屏幕图像

8

有没有一种编程方法可以在Mac上截取当前屏幕显示的截图(或以某种方式获取图像)?

最好使用C++而不是Objective-C。

3个回答

2

我一直在解决同样的问题。这是我搜索一段时间后想出来的代码。

CGImageRef screenShot = CGWindowListCreateImage( CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);

//std::string image_name = "/Users/nikhil/Desktop/SC_";
//image_name+=str;
//cout<<str<<endl;

//image_name+=".jpg";
//cout<<image_name<<endl;
CFStringRef file = CFSTR("/Users/nikhil/Desktop/SC.jpg");
CFStringRef type = CFSTR("public.jpeg");
CFURLRef urlRef = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, file, kCFURLPOSIXPathStyle, false );
CGImageDestinationRef image_destination = CGImageDestinationCreateWithURL( urlRef, type, 1, NULL );
CGImageDestinationAddImage( image_destination, screenShot, NULL );
CGImageDestinationFinalize( image_destination );

您需要向CFSTR()提供静态字符串。虽然我自己正在寻找一种替代方法,以时间戳为基础的文件名将屏幕捕捉保存到文件中。


0

我建议使用Quartz Display Services和函数CGDisplayCreateImage()


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接