1

I have an NSTextAttachment which contains an NSImage. What I would like to do is scale this image so that it's smaller in dimensions but will keeping the same number of pixels. Then moving it to the general pasteboard. Once it's pasted (into whatever app) I would like it to paste with the same dimensions I just set. The problem I am having is that pasting it in any other app always puts it at is original dimensions. I have tried the following things and none seem to fully work

First is simply resizing the image and then copying. This retains the proper dimensions (let's say 100 x 100 pt) but the image is very blurry and the resolution is 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()

The second thing I have tried is use the techniques listed on this page. None of which do anything once I paste the TextAttachment to another app (such as Pages for example). Subclassing NSTextAttachment has no effect. Also changing the bounds of the attachment seems to have no effect once it's pasted.

The last thing I tried is after the iterating through the NSAttributedString which holds the attachments, and setting each one's bounds. Still no effect when pasting into other apps.

So the ultimate question: is there a way to have a scaled NSImage inside and NSTextAttachment copy to the clipboard?

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])
}
Community
  • 1
  • 1
MAH
  • 841
  • 6
  • 16
  • How do you put the image on the pasteboard? – Willeke Dec 05 '16 at 17:24
  • @Willeke I've updated the original question to include that code. Thanks! – MAH Dec 06 '16 at 00:19
  • I think the original image from the file is copied to the pasteboard but I might be wrong. Try to make a copy of the NSImage or the NSImageRep without the reference to the file. – Willeke Dec 06 '16 at 01:33
  • @Willeke ah I should've mentioned. I gave it a try be creating a new `NSImage`, locking focus, drawing `im` into it and unlocking focus. Unfortunately the result is the same. The image is very blurry. – MAH Dec 06 '16 at 01:35
  • Make a copy with the same resolution and set the size of the copy. – Willeke Dec 06 '16 at 01:38
  • I just tried that. Doesn't seem to work. It has something to do with it being an NSTextAttachment. It always uses the pixel width / height it looks like. I've always tried manually created the RTF code which now lets you embed images if they're converted to base64 hex. Unfortunately, Apple doesn't support the \pict property. – MAH Dec 06 '16 at 01:42
  • Try `im.representations[0].size = NSMakeSize(100.0, 100.0)` – Willeke Dec 06 '16 at 23:27
  • @Willeke thanks for all the help. Unfortunately that still results in a very blurry image. – MAH Dec 06 '16 at 23:35

0 Answers0