我正在编写一个下载图像并打印它的 webkit 插件。我已经验证下载了正确的数据并且 NSImage 具有正确的尺寸。不幸的是,包含图像的 NSImageView 不显示任何内容,既不在发送到打印机的文档中,也不在包含视图的窗口中,因为它是内容视图的唯一子视图。这是我的代码:
-(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];
}
谢谢!