From 375c7d08d9928cdec24fae67328147381022fa39 Mon Sep 17 00:00:00 2001 From: ComplexSpaces Date: Fri, 12 Sep 2025 02:18:09 -0500 Subject: [PATCH] Return a conversion failure error when CGImageCreate fails --- src/platform/osx.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/platform/osx.rs b/src/platform/osx.rs index a2591ebd..31952b66 100644 --- a/src/platform/osx.rs +++ b/src/platform/osx.rs @@ -34,7 +34,7 @@ fn image_from_pixels( pixels: Vec, width: usize, height: usize, -) -> Retained { +) -> Result, Error> { use objc2::AllocAnyThread; use objc2_app_kit::NSImage; use objc2_core_foundation::CGFloat; @@ -70,6 +70,13 @@ fn image_from_pixels( let colorspace = unsafe { CGColorSpaceCreateDeviceRGB() }.unwrap(); + // XXX: If this returns an error, try running your application from the command line or + // use `Console.app`. For the later, make sure that before you start streaming log messages + // that Action -> `Include Info Messages` and Action -> `Include Debug Messages` are both + // enabled in the menubar. CoreGraphics will write debugging/error information to these places. + // + // - https://redsweater.com/blog/129/coregraphics-log-jam + // - https://github.com/1Password/arboard/issues/204 let cg_image = unsafe { CGImageCreate( width, @@ -85,10 +92,10 @@ fn image_from_pixels( CGColorRenderingIntent::RenderingIntentDefault, ) } - .unwrap(); + .ok_or(Error::ConversionFailure)?; let size = NSSize { width: width as CGFloat, height: height as CGFloat }; - unsafe { NSImage::initWithCGImage_size(NSImage::alloc(), &cg_image, size) } + Ok(unsafe { NSImage::initWithCGImage_size(NSImage::alloc(), &cg_image, size) }) } pub(crate) struct Clipboard { @@ -335,7 +342,7 @@ impl<'clipboard> Set<'clipboard> { #[cfg(feature = "image-data")] pub(crate) fn image(self, data: ImageData) -> Result<(), Error> { let pixels = data.bytes.into(); - let image = image_from_pixels(pixels, data.width, data.height); + let image = image_from_pixels(pixels, data.width, data.height)?; self.clipboard.clear();