我有 NSSplitView
,两侧都有两个 IKImageView
来相互比较。我正在尝试实现打印功能,但图像在预览时消失。 ( What should be printed 与 What 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 1 、 Method 2 ),使用单个 NSImageView
创建新的 viewController
,在那里显示我的屏幕截图,然后打印 imageView。但即使在屏幕截图中也看不到图像。
重要的是,如果我使用 NSImageViews
而不是 IKImageViews
,打印效果就很好。
我还能尝试什么?(objective-c
和 swift
解决方案都可以)
最佳答案
终于找到了解决方法(还考虑了缩放级别)。我从 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/