Skip navigation.
 
mlNewbie question: NSDocument and standard file formats
FROM : Andrew Ebling
DATE : Sun Nov 25 16:25:39 2007

Hello,

I'm trying to write my first Cocoa document-based application, which 
will manipulate PNG images.  At this stage, I'm trying to write an 
NSDocument subclass which will load data from a PNG image file and 
create an instance of NSImage, which will then be handed to a View 
class for display.

However, the existing "plumbing" provided by AppKit seems to assume 
that you are trying to read in documents which you have previously 
written out (i.e. proprietary formats which you have invented just for 
your application).  What I want to do is read an existing file format:

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:
(NSError **)outError
{
   NSImage *img = [[NSImage alloc] initWithData:data];
   [imageView setImage:img];
   [img release];

         return YES;
}

However, that naive code does not work, because the data contained in 
the NSData does not appear to be in a format that NSImage understands.

So I went digging in:

ADC Home > Reference Library > Guides > Cocoa > Design Guidelines > 
Document-Based Applications Overview > The Roles of Key Objects in 
Document-Based Applications

And read the following:

"When the user chooses Open from the File menu, 
theNSDocumentController object displays the Open panel, gets the 
user’s selection, finds the NSDocument subclass for the file (based on 
its document type information), allocates an instance of this class, 
and initializes the object and loads document data by invoking the 
NSDocument method initWithContentsOfURL:ofType:error:"

So my question is, without subclassing NSDocumentController (which I 
understand you are not supposed to do), how do I tell 
NSDocumentController to simply hand me back an instance of NSImage? 
Or is it even already doing this for me and all I have to do is get 
the class out of NSData?  If so how?

Thank you for your patience with this basic question.  At the moment I 
feel like I'm falling down the gap between finishing a book on Cocoa 
(Hillegass book in my case) and being confident/competent enough to 
make progress on a genuinely useful application.

best regards,

Andrew

Related mailsAuthorDate
mlNewbie question: NSDocument and standard file formats Andrew Ebling Nov 25, 16:25
mlRe: Newbie question: NSDocument and standard file formats Jerry Krinock Nov 26, 03:39
mlRe: Newbie question: NSDocument and standard file formats Heinrich Giesen Nov 26, 10:44
mlRe: Newbie question: NSDocument and standard file formats Andrew Ebling Nov 26, 14:45
mlRe: Newbie question: NSDocument and standard file formats Erik Buck Nov 26, 16:28
mlRe: Newbie question: NSDocument and standard file formats PGM Nov 26, 21:24