我有一个NSTextAttachment
包含一个NSImage
. 我想做的是缩放此图像,使其尺寸更小,但保持相同数量的像素。然后将其移至一般粘贴板。一旦它被粘贴(到任何应用程序中),我希望它以我刚刚设置的相同尺寸粘贴。我遇到的问题是将它粘贴到任何其他应用程序中总是将其放置在原始尺寸。我已经尝试了以下事情,但似乎没有一个完全有效
首先是简单地调整图像大小然后复制。这保留了适当的尺寸(比如说 100 x 100 pt),但图像非常模糊,分辨率为 72 dpi。
//variable im is an NSImage object that is present
//variable newsize is an NSSize which is calculated to give the image proper resolution
im.lockFocus()
im.size = newsize
im.unlockFocus()
我尝试的第二件事是使用此页面上列出的技术。一旦我将 TextAttachment 粘贴到另一个应用程序(例如 Pages),它们都不会做任何事情。子类化 NSTextAttachment 无效。粘贴后,更改附件的边界似乎也没有效果。
我尝试的最后一件事是在遍历NSAttributedString
包含附件并设置每个人的界限之后。粘贴到其他应用程序时仍然无效。
所以最终的问题是:有没有办法在里面有一个缩放的 NSImage 和 NSTextAttachment 复制到剪贴板?
func copy() {
let pboard = NSPasteboard.general()
pboard.clearContents()
let rtf = NSMutableAttributedString()
//self.images is an array that contains a bunch of images
//note that it is required that I use a RTF to copy/paste my images
//because there is text mixed with the images
//for simplicity I have removed the text from this code
//removing it from my app and doing the same code has no effect
for im in self.images {
let a = NSTextAttachment() //subclassing NSTextAttachment has no effect on pasted image size
a.image = im
a.bounds = NSMakeRect(0,0,100,100) //has no effect on pasted image size
//if I resize the image prior to copying (using the code above)
//the pasted image has correct dimensions but is very blurry.
let str = NSAttributedString(attachment: a)
rtf.append(str)
}
pboard.writeObjects([rtf])
}