终于找到了解决方法(同样考虑到了缩放级别)。我从IKImageViews
得到NSImages
,将它们传递给新的ViewController
,我在那里显示要打印的内容的预览。在该视图控制器中,我有一个简单的NSView
(rootView)和两个NSImagesViews
并排。我将我的图像从IkImageViews
设置为NSImageViews
,然后打印rootView。
这是我如何得到NSImages
从IkImageViews
:
步骤: 获取可见/裁剪/缩放从IkImageView
RECT;从IKImageView's
CGImageRef
创建NSImage
;创建新的NSImage
并在其中绘制裁剪图像。
注: OBJ-C语法可能是有点错了,我从来没有写过之前(Xamarin开发)OBJ-C的连一行。
的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;
}
希望有人会找到答案有用。
是不是只是做一个指针,然后分配一个IKImageView对象到打印操作方法的问题,因为IKImageView是NSView的子类? –
也许有关:[是否有可能以编程方式在Cocoa中打印IKImageBrowserView?](http://stackoverflow.com/questions/10648544/is-it-possible-to-print-ikimagebrowserview-in-cocoa-programmatically) – Willeke
@ElTomato我在'NSSplitView'中并排放置了两个'IKImageViews',我需要一起打印。 – arsena