1

I have NSSplitView with two IKImageView on both sides to compare them with each other. I'm trying to implement Print functionality but images disappear on preview. (What should be printed vs What I get)

What I tried:

Based on docs, locking focus on splitView and directly printing it with

[[NSPrintOperation printOperationWithView:view] runOperation];

But as long as docs state that

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

I tried making a "screenshot"(Method 1, Method 2) of the splitView, creating new viewController with single NSImageView, displaying my screenshot there and then printing imageView. But images weren't visible even in screenshot neither.

Important thing is that printing works just fine if I use NSImageViews instead of IKImageViews.

What else can I try?(Both objective-c and swift solutions would work)

Community
  • 1
  • 1
arsena
  • 1,935
  • 19
  • 36
  • Isn't it just the matter of making a pointer and then assigning an IKImageView object to the print operation method since IKImageView is a subclass of NSView? – El Tomato Aug 18 '16 at 12:51
  • Maybe related: [Is it possible to print IKImageBrowserView In Cocoa programmatically?](http://stackoverflow.com/questions/10648544/is-it-possible-to-print-ikimagebrowserview-in-cocoa-programmatically) – Willeke Aug 18 '16 at 22:33
  • @ElTomato I have two `IKImageViews` placed side by side in `NSSplitView` and I need to print both together. – arsena Aug 19 '16 at 07:36
  • @Willeke Tried that but I get an exception in unmanaged code at `img.Draw` call. – arsena Aug 19 '16 at 14:09
  • Print a temporary splitview with NSImageViews? – Willeke Aug 19 '16 at 23:50
  • I was thinking of that exactly but want to try every other possibility before makind a radical decision :d – arsena Aug 21 '16 at 03:42
  • @Willeke I did it at last. You can check the answer – arsena Aug 22 '16 at 10:37

1 Answers1

0

Finally found a workaround(Also takes into account a zoom level). I get NSImages from IKImageViews, pass them to new ViewController where I show a preview of what will be printed. In that viewcontroller I have a simple NSView(rootView) with two NSImagesViews side by side. I set my images from IkImageViews to NSImageViews and then print the rootView.

Here is how I get NSImages from IkImageViews:

Steps: Get visible/cropped/zoomed rect from IkImageView; Create NSImage from IKImageView's CGImageRef; Create new NSImage and draw cropped image into it.

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;
    }

Hope anyone will find the answer useful.

arsena
  • 1,935
  • 19
  • 36