views:

290

answers:

1

I'm writing a webkit plugin that downloads an image and prints it.I have verified that the correct data is downloaded and the NSImage has the correct dimensions. Unfortunately, the NSImageView containing the image doesn't display anything, neither in the document sent to the printer nor a window containing the view as it's content view's only subview. Here's my code:

-(void)printImageAtURL:(NSString*)imageUrl{ 
    NSURLResponse *response;
    NSError *error = nil;

    NSURL *url = [NSURL URLWithString:imageUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if(error)
         return;
    //Data is the correct length
    NSImage *image = [[NSImage alloc] initWithData:data];

    NSSize imageSize = [image size];
    //imageSize is correct dimensions

    NSRect viewRect = NSMakeRect(0, 0, imageSize.width, imageSize.height);

    NSImageView *imageView = [[NSImageView alloc] initWithFrame:viewRect];
    [imageView setImage:image];
    [image release];

    NSPrinter *printer = [NSPrinter printerWithName:[[NSPrinter printerNames] objectAtIndex:0]];
    NSPrintInfo *printInfo = [[NSPrintInfo alloc] init];
    [printInfo setPrinter:printer];

    NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:imageView printInfo:printInfo];
    [printOp setShowsPrintPanel:NO];
    [printOp runOperation];
    //The print job is an empty page :(

    //Pop-up a window just to test the image view
    NSWindow * window = [[NSWindow alloc] init];
    [window makeKeyAndOrderFront:self];
    [window setFrame:viewRect display:YES];
    [[window contentView] addSubview:imageView];
    //The window is sized correctly but empty :(

    [printInfo release];
    [imageView release];

}

Thanks!

A: 

You probably solved it by now, but.

  1. Try to load a local file (ie, not network). Maybe there are some encoding issues somewhere and NSImage can't properly parse the file?

  2. Try using NSImageCell instead of NSImageView.

Looks like imageView isn't right, because it fails on screen and in print..

HTH

EightyEight