Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 在NSPasteboard中缩放NSTextAttachment_Cocoa_Nstextattachment - Fatal编程技术网

Cocoa 在NSPasteboard中缩放NSTextAttachment

Cocoa 在NSPasteboard中缩放NSTextAttachment,cocoa,nstextattachment,Cocoa,Nstextattachment,我有一个NSTextAttachment,其中包含一个NSImage。我想做的是缩放这个图像,使它的尺寸更小,但将保持相同的像素数。然后将其移动到通用粘贴板。一旦它粘贴(到任何应用程序)我希望它粘贴与我刚才设置的尺寸相同。我遇到的问题是,将它粘贴到任何其他应用程序中都会使它处于原始尺寸。我试过以下方法,但没有一种完全奏效 首先是简单地调整图像大小,然后复制。这保留了适当的尺寸(比如100 x 100 pt),但图像非常模糊,分辨率为72 dpi //variable im is an NSIma

我有一个
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()
我尝试过的第二件事是使用上面列出的技术。一旦我将文本附件粘贴到另一个应用程序(例如页面),这些应用程序都不会执行任何操作。子类化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])
}

你是如何将图像放在粘贴板上的?@Willeke我已经更新了原始问题以包含该代码。谢谢我认为文件中的原始图像被复制到了粘贴板上,但我可能错了。尝试在不引用文件的情况下复制NSImage或NSImageRep。@Willeke啊,我应该提到的。我尝试创建一个新的
NSImage
,锁定焦点,将
im
绘制到其中并解锁焦点。不幸的是,结果是一样的。图像非常模糊。制作一份分辨率相同的副本并设置副本的大小。如何将图像放在粘贴板上?@Willeke我已更新了原始问题以包含该代码。谢谢我认为文件中的原始图像被复制到了粘贴板上,但我可能错了。尝试在不引用文件的情况下复制NSImage或NSImageRep。@Willeke啊,我应该提到的。我尝试创建一个新的
NSImage
,锁定焦点,将
im
绘制到其中并解锁焦点。不幸的是,结果是一样的。图像非常模糊。请制作一份分辨率相同的副本,并设置副本的大小。