INNER CODE UNIT · Rust
system_copy_to_pasteboard
autopilot-rs/autopilot-rs · src/bitmap.rs:376
fn system_copy_to_pasteboard(&self) -> ImageResult<()> {
use objc2::AnyThread;
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2_app_kit::{NSImage, NSPasteboard, NSPasteboardWriting};
use objc2_foundation::{NSData, NSArray};
use image::ImageFormat;
let mut buffer: Vec<u8> = Vec::new();
let mut cursor = std::io::Cursor::new(&mut buffer);
self.image.write_to(&mut cursor, ImageFormat::Png)?;
unsafe {
let data = NSData::dataWithBytes_length(
buffer.as_ptr() as *const std::ffi::c_void,
buffer.len(),
);
let image: Retained<NSImage> = NSImage::initWithData(NSImage::alloc(), &data).ok_or(
std::io::Error::other("Failed to create NSImage from NSData")