如何解决将旋转后的 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 举报,一经查实,本站将立刻删除。