nashaofu / xcap

XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (to be implemented).
https://docs.rs/xcap
Apache License 2.0
459 stars 55 forks source link

MacOS下窗口截图非常慢 #110

Closed atopx closed 6 months ago

atopx commented 6 months ago

我不了解正常效率问题还是环境问题,有没有解决办法

基础环境: MacOS 12.6.8 Apple Silicon rustc 1.73.0 xcap = "0.0.5"

代码输出:

❯ ./target/debug/examples/screen         
ID:89470 截图耗时: 1.607249625s
运行耗时: 1.72181875s

代码:

use std::time::Instant;
use xcap::Window;

fn main() {
    let start = Instant::now();

    let title = "-zsh";

    let windows = Window::all().unwrap();
    for window in windows {
        if window.is_minimized() || window.title() != title {
            continue;
        }
        let start2 = Instant::now();
        let image = window.capture_image().unwrap();
        image.save("target/iTerm.png").unwrap();
        println!("ID:{} 截图耗时: {:?}", window.id(), start2.elapsed());
    }

    println!("运行耗时: {:?}", start.elapsed());
}
nashaofu commented 6 months ago

release 模式试试呢?

atopx commented 6 months ago

在release下好很多,只有在debug模式下 image.save()这里慢,其他速度都正常