将旋转后的 NSImageView 作为子视图的 NSView 保存到 pdf 文件的问题 - NSImageView 未旋转

问题描述

我有 NSView,我正在以编程方式添加一个 NSImageView(或 NSTextField)作为子视图。然后我通过设置 frameCenterRotation 的角度来旋转这个子视图。 NSImageView 的图片被旋转了。

myNSImageView.frameCenterRotation = angle

但是,当我尝试打印 NSView 或将其保存为 pdf 文件时,此旋转不适用于 NSImageView 子视图。在打印预览或 pdf 文件中,图像不会旋转。

如何完全按照屏幕上的旋转图像保存/打印 NSView?

更新。更多细节。 NSView 的代码。对于这个 NSview .wantsLayer = true

NSImage 是从 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 被这些鼠标事件旋转:

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
}

并打印 myNSView(在基于文档的应用程序中)

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
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)