我将Cocoa代码迁移到Cocoa+ JNI,因为Cocoa是不推荐的。代码打印存储在文件中的图像。新的可可编码基本上是:
NSImage *image = [[NSImage alloc] initWithContentsOfFile:spoolFile];
if ( [image isValid] ) {
NSImageView *view = [[NSImageView alloc] init];
[view setImage:image];
[view setImageScaling:NSScaleProportionally];
NSPoint p;
NSSize s;
p.x = static_cast<float>( boundsX );
p.y = static_cast<float>( boundsY );
[view setBoundsOrigin:p];
s.width = static_cast<float>( boundsWidth );
s.height = static_cast<float>( boundsHeight );
[view setBoundsSize:s];
NSPrintInfo *info = [NSPrintInfo sharedPrintInfo];
[info setHorizontalPagination:NSClipPagination];
[info setVerticalPagination:NSClipPagination];
[info setHorizontallyCentered:NO];
[info setVerticallyCentered:NO];
p.x = static_cast<float>( boundsX );
p.y = static_cast<float>( [info paperSize].height - boundsHeight - boundsY );
[view translateOriginToPoint:p];
NSPrintOperation *printOp =
[NSPrintOperation printOperationWithView:view printInfo:info];
[printOp setShowsPrintPanel:NO];
[printOp runOperation];
}运行此代码时,最终会发生以下情况:
Thread 0 Crashed:
0 com.apple.AppKit 0x9484ac75 -[NSConcretePrintOperation(NSInternal) _tryToSetCurrentPageNumber:] + 345
1 com.apple.AppKit 0x948d88cf -[NSView(NSPrintingInternal) _printForCurrentOperation] + 524
2 com.apple.AppKit 0x948d85c5 -[NSConcretePrintOperation _renderView] + 358
3 com.apple.AppKit 0x9491f0c0 -[NSConcretePrintOperation runOperation] + 362为什么?如何简单地打印存储在文件中的图像?
发布于 2009-09-16 20:25:41
NSImageView *view = [NSImageView alloc init];
那是无效的。您需要使用initWithFrame:来初始化视图。您可能希望传递一个由NSZeroPoint和图像大小组成的帧。
至于setBoundsOrigin:和setBoundsSize:的使用:假设您打算裁剪图像,我不确定这些操作是否有效。您可以尝试它们(在解决了上面的问题之后),但是我会觉得从旧的部分创建一个新的映像更安全。要做到这一点,您可以创建一个所需大小的空图像,锁定焦点,在新图像的原点绘制旧图像的正确部分,并在新图像上解锁焦点,然后将新图像而不是旧图像赋予图像视图。
https://stackoverflow.com/questions/1434175
复制相似问题