• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
The right way to put a JPEG, PNG, GIF, etc. onto the pasteboard
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The right way to put a JPEG, PNG, GIF, etc. onto the pasteboard


  • Subject: The right way to put a JPEG, PNG, GIF, etc. onto the pasteboard
  • From: email@hidden
  • Date: Tue, 9 Apr 2002 17:00:30 -0700

There are standard pasteboard types for TIFF, PICT, PDF and EPS. But there aren't any (or at least, they're not documented in NSPasteboard's doc) for JPEG, PNG, GIF, and other such types.
So the question is, what's the best way to represent these image types on the pasteboard? Several different ways occur to me:

1. Use NSFileContentsPboardType, supply an HFS type and a preferred file name with an extension that identifies the type of image data properly, and use writeFileWrapper: to put the image data onto the pasteboard.

2. Use NSFilenamesPboardType, having first saved the image data out to uniquely named files under /tmp.

3. Use NSRTFDPboardType, having wrapped the image into an attributed string first.

I tried #1, which seemed like the "right" way to do it, and I think my code is correct. This snippet assumes the existence of NSData *data and NSPasteboard *pasteboard.

NSFileWrapper *wrap = [[NSFileWrapper alloc] initRegularFileWithContents:data];
NSMutableDictionary *wrapDict = [NSMutableDictionary dictionary];

[wrapDict setObject:[NSNumber numberWithUnsignedLong:FOUR_CHAR_CODE('SSct')] forKey:NSFileHFSCreatorCode];
[wrapDict setObject:[NSNumber numberWithUnsignedLong:FOUR_CHAR_CODE('JPEG')] forKey:NSFileHFSTypeCode];
[wrapDict setObject:NSFileTypeRegular forKey:NSFileType];
[wrapDict setObject:[[wrap fileAttributes] objectForKey:NSFilePosixPermissions] forKey:NSFilePosixPermissions];

[wrap setFileAttributes:wrapDict];
[wrap setPreferredFilename:@"clipboard.jpg"];

[pasteboard writeFileWrapper:wrap];
[wrap release];

However, this gives me a pasteboard which doesn't paste anything into TextEdit. The pasteboard I've created here logs these as its types:

0 : <CFString 0x1858930 [0x8016024c]>{contents = "NXTypedFileContentsPboardType:jpg"}
1 : <CFString 0x1f15ce0 [0x8016024c]>{contents = "NXFileContentsPboardType"}

Which looks right. But if TextEdit and other Cocoa apps can't recognize the data, it doesn't much matter whether it's technically right or not. So I'm hoping for a little guidance on how to make this work better. Anyone?

Ben Haller
Stick Software
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: The right way to put a JPEG, PNG, GIF, etc. onto the pasteboard
      • From: email@hidden
  • Prev by Date: Re: Can I do something when double click on a button?
  • Next by Date: Re: Can I do something when double click on a button?
  • Previous by thread: -keep_private_externs defined?
  • Next by thread: Re: The right way to put a JPEG, PNG, GIF, etc. onto the pasteboard
  • Index(es):
    • Date
    • Thread