Problem to save to pdf file a NSView with rotated NSImageView as subview - NSImageView is not rotated

132 Views Asked by At

I have NSView and I am adding programatically a NSImageView (or NSTextField) as a subview. Then I do rotation of this subview by setting the angle for frameCenterRotation. The image of NSImageView is rotated.

myNSImageView.frameCenterRotation = angle

But, when I try to print NSView or save it to pdf file this rotation is not applied to the NSImageView subview. In print preview or in pdf file the image is not rotated.

How to save/print NSView with rotated image exactly as it is on the screen ?

Update. More details. Code for the NSView. For this NSview .wantsLayer = true

NSImage is added from NSURL

override func performDragOperation(_ sender: NSDraggingInfo) -> Bool
{
    guard let pasteboardObjects = sender.draggingPasteboard.readObjects(forClasses: [NSURL.self], options: nil), pasteboardObjects.count > 0 else {
        return false
    }
    
    sender.draggingDestinationWindow?.orderFrontRegardless()
    let mousePoint = self.convert(sender.draggingLocation, from: nil)
    
    for each in pasteboardObjects
    {
        if let url = each as? NSURL
        {
            if let image = NSImage(contentsOfFile: url.path!)
            {
                let constrainedSize = image.representations.first!.size
                let imageView = NSImageView(frame:NSRect(x: mousePoint.x - constrainedSize.width/2, y: mousePoint.y - constrainedSize.height/2, width: constrainedSize.width, height: constrainedSize.height))
                imageView.image = image

                arrayOfNSImageViews.append(imageView)
                self.addSubview(imageView)
            }
        }
    }
    
    return true
}

NSImageView is rotated by these mouse events:

override func rightMouseDown(with event: NSEvent)
{
    mouseDownPointRight = self.convert(event.locationInWindow, from: nil)
            
    for i in (0..<arrayOfNSImageViews.count).reversed()
    {            
        if arrayOfNSImageViews[i].frame.contains(mouseDownPointRight) == true
        {
            imageViewToRotate = arrayOfNSImageViews[i]                
            break
        }
    }                    
}

    
override func rightMouseDragged(with event: NSEvent)
{
    let draggedMousePoint = self.convert(event.locationInWindow, from: nil)
                    
    angle = draggedMousePoint.y - mouseDownPointRight.y / 1000

    imageViewToRotate.frameCenterRotation = angle
}

And printing myNSView (in document based app)

override func printOperation(withSettings printSettings: [NSPrintInfo.AttributeKey : Any]) throws -> NSPrintOperation
{
    self.printInfo.isHorizontallyCentered = true
    self.printInfo.isVerticallyCentered = true
    self.printInfo.verticalPagination = .fit
    self.printInfo.horizontalPagination = .fit
            
    let printOperation: NSPrintOperation = NSPrintOperation(view: myNSView, printInfo: self.printInfo)
    printOperation.showsPrintPanel = true

    return printOperation
}
0

There are 0 best solutions below