objective-c - IKImageView 打印时图像不可见

标签 objective-c macos cocoa xamarin.mac

我有 NSSplitView ,两侧都有两个 IKImageView 来相互比较。我正在尝试实现打印功能,但图像在预览时消失。 ( What should be printedWhat I get )

我尝试过的:

基于docs ,锁定splitView焦点并直接用

打印

[[NSPrintOperation printOperationWithView:view] runOperation];

但只要文档说明

If UI contains multiple views that can have focus view-based printing doesn’t work well.

我尝试制作 splitView 的“屏幕截图”( Method 1Method 2 ),使用单个 NSImageView 创建新的 viewController ,在那里显示我的屏幕截图,然后打印 imageView。但即使在屏幕截图中也看不到图像。

重要的是,如果我使用 NSImageViews 而不是 IKImageViews,打印效果就很好。

我还能尝试什么?(objective-cswift 解决方案都可以)

最佳答案

终于找到了解决方法(还考虑了缩放级别)。我从 IKImageViews 获取 NSImages,将它们传递给新的 ViewController,在其中显示将要打印的内容的预览。在该 View Controller 中,我有一个简单的 NSView(rootView),并排有两个 NSImagesViews。我将图像从 IkImageViews 设置为 NSImageViews,然后打印 rootView。

以下是我如何从 IkImageViews 获取 NSImages:

步骤: 从 IkImageView 获取可见/裁剪/缩放的矩形;从IKImageView的CGImageRef创建NSImage;创建新的 NSImage 并将裁剪后的图像绘制到其中。

Note: obj-c syntax might be a bit wrong, I have never written even a single line of obj-c before(Xamarin Developer).

Objective-c:

NSRect croppedRect = [imageView convertViewRectToImageRect:[imageView visibleRect]];
NSImage srcImage = [[NSImage alloc] initWithCGImage:[imageView image] size:NSZeroSize];
NSImage croppedImage = [[NSImage alloc] initWithSize:croppedRect.size];
[croppedImage lockFocus];
[srcImage drawAtPoint:NSZeroPoint
          fromRect:croppedRect
          operation:NSCompositeCopy
          fraction:1.0];
[croppedImage unlockFocus];
return croppedImage;

C#(Xamarin):

NSImage GetImageToPrint()
    {
        var cropRect = LeftImage.ConvertViewRectToImageRect(LeftImage.VisibleRect());

        NSImage srcImage = new NSImage(LeftImage.Image, LeftImage.ImageSize);
        NSImage croppedImage = new NSImage(cropRect.Size);
        croppedImage.LockFocus();
        srcImage.Draw(CGPoint.Empty, cropRect, NSCompositingOperation.Copy, 1.0f);
        croppedImage.UnlockFocus();

        return croppedImage;
    }

希望任何人都会发现答案有用。

关于objective-c - IKImageView 打印时图像不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39013090/

相关文章:

cocoa - 在 void 指针上使用桥转换的 CoreMIDI 回调函数问题

iphone - 我应该使用 C 函数还是 Obj-C 方法?

cocoa - 自定义 NSLog,使其显示更少的信息

ios - objective-c : Accessing properties of an object instance that's in an array that's a property of an object

ruby - OS X Yosemite ffi-hunspell dyld 惰性符号绑定(bind)失败 : symbol not found

iphone - 将所有日历日期插入到 uitableview 中。

ios - 在 UIScrollView 仍处于事件状态的情况下检测 View 中的连续触摸位置

python - OS X python : can I explicitly set MACOSX_DEPLOYMENT_TARGET for extensions?

objective-c - 分发配置文件有效,但开发配置文件显示未找到有效的签名实体

ios - 更新 EKEvent 时收到间歇性 EXC_BAD_ACCESS