0

I am trying to make a simple document-based cocoa application that can save and load images in pdf files.

For the save part, I am using

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    return [imageView dataWithPDFInsideRect:[imageView bounds]];
}

And this works, the image could be saved to a PDF file.

For the load part, I am using

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    NSData *dataFromFile = [data retain];
    NSImage *image = [[NSImage alloc] initWithData:dataFromFile];
    NSLog(@"Load image as: %@", image);
    // Do something about the image
    if (outError != NULL) {
        NSLog(@"Error when loading data ...");
        *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
        return NO;
    }
    return YES;
}

This always fails, but the NSLog prints out that the image is not NULL:

Load image as: NSImage 0x16ead890 Size={1023, 601} Reps=(NSPDFImageRep 0x16e97480 Size={1023, 601} ColorSpace=NSCalibrateRGBColorSpace BPS=0 Pixels=1023x601 Alpha=NO)

Error when loading data ...

I don't quite understand what problem happens in the readFromData that makes outError != NULL here.

ccy
  • 13
  • 4
  • Finding out what the error is would be a good first step. – Azeem.Butt Dec 26 '09 at 03:36
  • The problem is solved. The error handling is incorrect. But one more problem is: I was trying to update a view (which belongs another class) from the loaded image, the view had not been initialised by the time readFromData: was being called, so I was sending the message to a nil object. So I declare a new NSImage property in the class to keep the image loaded from readFromData: and update the view in windowControllerDidLoadNib:. It works. Any simpler way to do this? – ccy Dec 26 '09 at 08:00

3 Answers3

1

Your image is being successfully created.

You're not quite understanding how the error parameter works. Your -readFromData:ofType:error: method is handed a pointer to a pointer, for you to use if you fail at creating the image from the NSData instance.

You should read the documentation on NSError to get your head around how to create and use NSError instances.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • Better yet, the Error Handling Programming Guide for Cocoa: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ Particularly the “Using and Creating Error Objects” section. – Peter Hosey Dec 26 '09 at 06:22
0

You're passing outError from whatever is calling -readFromData:ofType:error:. What's more, outError is a pointer-to-a-pointer. What you should check is:

if (outError != nil && *outError != nil) {...
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • It doesn't work even after remove the whole "if (outError != NULL)" part. The previously saved pdf image is loaded as empty image. – ccy Dec 26 '09 at 04:00
0

outError is there for you to set if you have an error reading the document. It doesn't tell you whether your code failed or not. It's how you tell the caller what went wrong. [NSImage initWithData:] returns nil if it fails, so you need to change:

if (outError != NULL) {

to:

if (image == NULL) {
Dewayne Christensen
  • 2,084
  • 13
  • 15