views:

340

answers:

2

I'm trying to copy a CGImageRef to the clipboard pasteboard. I found a function that claims it should do this by creating a destination from (zero sized), adding the image to the destination, finalizing, then PasteboardPutItemFlavor the ref into the clipboard.

However it doesn't work, so two questions:

  1. Is this the correct way to go about this? (ie, is there just a small bug, or am I doing it wrong?)

  2. What type should I make the destination? The source had it as TIFF, but word doesn't seem to know how to deal with that, I changed it to PICT, which at least gave me the "paste" option, but then said it was too big...

Code:

void copyCGImageRefToPasteboard(CGImageRef ref)
{
    OSStatus err = noErr;
    PasteboardRef theClipboard;

    err = PasteboardCreate( kPasteboardClipboard, &theClipboard );
    err = PasteboardClear( theClipboard );// 1

    CFMutableDataRef url = CFDataCreateMutable(kCFAllocatorDefault, 0);

    CFStringRef type = kUTTypePICT;
    size_t count = 1;
    CFDictionaryRef options = NULL;
    CGImageDestinationRef dest = CGImageDestinationCreateWithData(url, type, count, options);
    CGImageDestinationAddImage(dest, ref, NULL);
    CGImageDestinationFinalize(dest);

    err = PasteboardPutItemFlavor( theClipboard, (PasteboardItemID)1, type, url, 0 );
}
A: 

Ok, I'm answering my own question here, but here's what I've found:

Apple wants you to use PDF for pasteboards. So if you swap out Pict with PDF, it pretty muc just works. However, MS Word (what I was testing with) only started to allow pasting of PDF in the newest version (Which I don't have).

So, that's the solution, use PDF, and require Word 2008.

Brian Postow
You should be able to put PICT data on the pasteboard just as well. Please see my comment to your question.
Peter Hosey
I think that the problem is that the CG functions don't want to create PICTs... so there's no dictionaries for that... anyway, doing everything as PDF works, and seems to be the way of the future...
Brian Postow
A: 

Enter "The Cupertino Tongue Twister" by John Dempsey

Peter put a PICT upon the pastboard.

Deprecated PICT's a poor pasteboard type to pick.

For reference see: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/PasteboardGuide106/Articles/pbUpdating105.html

In short: it's deprecated to put PICT on the pasteboard.

nschmidt
excellent. Now I need to convince my BOSS of this! B-)
Brian Postow