macos - CGWindowListCreateImage 产生损坏的图像
问题描述
目标:我想定期在 OSX 中制作窗口截图。
遵循 Apple 指南,我设法构建了可以很好地捕获窗口内容的简单工作代码。但是,问题是大约 50 张图片中的 1 张由于某种原因而失真。
我正在使用CGWindowListCreateImage函数来定期捕获窗口内容,但它会不时产生垃圾。
这是我的简单代码:
CGImageRef img = CGWindowListCreateImage(CGRectNull,
kCGWindowListOptionIncludingWindow,
wc->window.window_id, wc->image_option);
if (!img)
return;
char path_str[1024];
snprintf(path_str, 1023, "/tmp/obs/image%llu.png", ts);
// here is just output image to file "as is"
CFURLRef path =
CFURLCreateWithFileSystemPath(NULL,
__CFStringMakeConstantString(path_str),
kCFURLPOSIXPathStyle, false);
// file/format to save pixels to
CGImageDestinationRef destination =
CGImageDestinationCreateWithURL(
path, CFSTR("public.png"), 1, NULL); //[4]
// add our captured pixels
CGImageDestinationAddImage(destination, img, nil);
// generate the image
if (!CGImageDestinationFinalize(destination)) {
printf("Failed to finalize\n");
}
我对不同的应用程序和窗口进行了测试,所以结果如下所示:
一张好照片(我对上面代码的期望)
残破的照片(这样的照片是偶然拍到的~1/50)
我已经尝试过的:
- 玩参数CGWindowImageOption
- 改为使用CGWindowListCreateImageFromArray
- 将图像输出到屏幕上,我看到了相同的结果。
在文件上建立CGImageRef,而不是使用CGWindowListCreateImage捕获- 工作正常。
使用的配置:
- macOS 10.13.6
- XQuartz 2.7.11
- 核心图形 2.0
在这一点上,我猜CGWindowListCreateImage是错误的或什么的。有没有人看到类似的东西?
解决方案
推荐阅读
- asp.net-mvc - 根据列值隐藏 div
- c# - 将特定表 ID 值传递给 Modal 的 AJAX 请求
- amazon-web-services - 将数据从一个 AWS 账户的 S3 存储桶复制到另一个 AWS 账户的 S3 存储桶
- reactjs - 如何从chartjs2堆叠图表中消除所有边框
- uml - UML - 是否可以将 ActivityPartitions 添加到 StructuredActivityNodes?
- python - 如何转换变量中的列表元素
- node.js - 如何在 foreach 循环中使用多个异步等待调用将返回值推送到数组中
- python - python中某些字符类型的最长连续子字符串
- javascript - 使用 webpack [request] 魔术注释将动态模块捆绑在一起
- python - 如何在由第二列定义的固定间隔内对一列的元素求和?