Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating a file containing both an image and text



On 12 Jan 08, at 13:50, Arthur C. wrote:
I would like to save screenshots from my program (as an image), with additional text data underneath, in the same file.
Using the OS-X 'screencapture' utility I can either create a PNG or PDF file, or send one of these to the clipboard.
I found several classes to import data from the clipboard (NSPasteBoard), and the NSAttributedString class which can write an RTFD file. That would be OK for this purpose. However, currently I can't get it to work.


Question: how can I store an image and some text in one file in the most convenient (simple) way? Which file format should it be, PDF or RTFD, or something else?

The following code extracts the (pdf) data from the pasteboard, and then attempts to create an RTFD file wrapper out of it.

NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSArray *supportedTypes = [NSArray arrayWithObject: NSPDFPboardType];
NSString *bestType = [pb availableTypeFromArray:supportedTypes];
if (bestType != nil){
NSData * RTFDData = [pb dataForType: NSPDFPboardType];
NSError * error;
NSDictionary * dict;
NSMutableAttributedString * myString = [[NSMutableAttributedString alloc] initWithData: RTFDData options:nil documentAttributes:&dict error:&error];
NSFileWrapper * fileWrapper = [myString RTFDFileWrapperFromRange:NSMakeRange(0, [myString length]) documentAttributes: dict];
[fileWrapper writeToFile: [NSString stringWithFormat:@"%@/ screenshot.rtfd", myCurrentDirectoryPath] atomically: YES updateFilenames: YES];
}


NSMutableAttributedString's initWithData call gives 'plain text' back in the documentAttributes dict, meaning it didn't recognize the PDF type. The rest does not produce a sensible RTFD file.

What you're doing in this code doesn't make sense. You can't turn a PDF into an RTFD just by throwing the raw PDF data at [NSMutableAttributedString initWithData:] and shouting "Catch!" (metaphorically). I haven't used the text system enough to be exactly sure of the details, but I believe you'll have to create a text attachment for the image to go into.


Also, I'm not sure why you're using the screencapture utility here. You can ask a window's contentView to render itself to an image, which is a lot "friendlier" than overwriting the user's pasteboard with your screenshot. Alternatively, if you're on 10.5, there are some new APIs (CGWindowListCreateImage) that'll let you capture a window directly to an image.
_______________________________________________


Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Creating a file containing both an image and text (From: Arthur C. <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.