Re: Overriding [NSDocument saveDocumentTo]
Re: Overriding [NSDocument saveDocumentTo]
- Subject: Re: Overriding [NSDocument saveDocumentTo]
- From: Greg Titus <email@hidden>
- Date: Wed, 1 Aug 2001 11:07:13 -0700
On Wednesday, August 1, 2001, at 01:51 AM, Ken Tabb wrote:
Hi,
following up from a previous message a couple of days ago, I'm trying to
add the ability to export TIFF stills from a QuickTime movie.
I was previously (erroneously, but it made it easy 8^) overriding
[myDocument dataRepresentationOfType:] to return the TIFF representation
of the pic, thus:
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
return [[myImageView image]TIFFRepresentation];
}
but after some kind soul pointed out the obvious to me (for which I
thank
them), I need to be overriding [NSDocument saveDocumentTo:] if I want
export features.
If you want to export as different types, all you need to do is add
those document types to the "Document Types" section of "Application
Settings", and make the -dataRepresentationOfType: method look for that
type and return the correct data.
To be more specific:
1) Select your application target in PB, then choose the "Application
Settings" tab.
2) In the "Document Types" section, fill in something like "TIFF" for
the name, "tiff" for the extensions, make sure the role is "None" and
then hit the Add button.
This will tell the NSDocument architecture that your app knows about a
document type called "TIFF", whose files have the extension "tiff". But,
it can't edit or open those files to view them (since the role is None).
3) Hit the "Expert" button for "Application Settings". This is something
that should be in the simple settings but isn't yet. Look under
"CFBundleDocumentTypes" - that's the key that all the document type
information is saved under. Look at the "0" dictionary, which will be
the information for your main document type (the Quicktime movie).
4) Click on "New Child", name the child "NSExportableAs" and make it an
array. This is the key that the NSDocument architecture looks for to
save documents as different types.
5) Select the "NSExportableAs" and click on "New Child" again. Set the
value to be a String and name it "TIFF" (or whatever the name of the
document type is that you added in step 2). This tells the app that
your Quicktime movie can be exported as a TIFF.
6) Change your -dataRepresentationOfType: method to look at the type and
return the TIFF data when it is asked for the "TIFF" type:
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
if ([aType isEqualToString:@"TIFF"])
return [[myImageView image] TIFFRepresentation];
else
... save the whole movie, or whatever your normal document
type does...
}
When you do things this way the NSDocument archictecture will
automatically add a 'File Format' popup to the save sheets. And
whichever format you select, that's the type that will be passed into
your -dataRepresentationOfType: method. The app will automatically name
the saved file with the correct extension and so on.
Things will work now, but you may want to take a couple additional steps:
7) The file format popup will show up even with "Save" and "Save As",
not just "Save To" - but every item in the popup except for your main
file format will be disabled. A couple early releases of OmniOutliner
did things this way, and we got a lot of e-mail from confused users who
thought that they couldn't export as different formats because they
could see the formats and couldn't select them (they didn't notice "Save
To"). So, first, you may want to change the "Save To" menu item to
"Export To" in Interface Builder.
8) Also you may want to get rid of that file format popup altogether in
the "Save" and "Save As" case - since it is pretty useless then. (It
would be nice if Apple did this themselves in the NSDocument code -
removed the popup if there was only one choice, so as not to confuse
users.) You can do that by overriding a couple methods in your
NSDocument subclass. Override -saveDocument:, -saveDocumentAs: and
-saveDocumentTo: to store which type of save operation you are doing
(then call the superclass implementation). Also override
-prepareSavePanel: to do something like this:
- (BOOL)prepareSavePanel:(NSSavePanel *)savePanel;
{
[super prepareSavePanel:savePanel];
if (saveOperation == NSSaveOperation || saveOperation ==
NSSaveAsOperation)
[savePanel setAccessoryView:nil];
return YES;
}
The -setAccessoryView:nil will remove the file format popup when you are
doing a save or save as.
Hope this helps,
-Greg