Cocoa 打印NSImage

Cocoa 打印NSImage,cocoa,printing,nsimage,Cocoa,Printing,Nsimage,我正在将Cocoa Java代码迁移到Cocoa+JNI,因为Cocoa Java已被弃用。该代码打印存储在文件中的图像。新的可可守则基本上是: NSImage *image = [[NSImage alloc] initWithContentsOfFile:spoolFile]; if ( [image isValid] ) { NSImageView *view = [[NSImageView alloc] init]; [view setImage:image];

我正在将Cocoa Java代码迁移到Cocoa+JNI,因为Cocoa Java已被弃用。该代码打印存储在文件中的图像。新的可可守则基本上是:

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];
}
为什么??如何简单地打印存储在文件中的图像?

这是您第二次指定给
p.y
时的打字错误吗?直到两行之后,您才开始定义
info

另外,使用和传递int不是比手工构造和使用
static\u cast
更简单吗?这似乎是一个非常简单的C++方法…… 例如,像这样的东西可以工作吗

NSImage *image = [[NSImage alloc] initWithContentsOfFile:spoolFile];
if ([image isValid]) {
    NSPrintInfo *info = [NSPrintInfo sharedPrintInfo];
    [info setHorizontalPagination:NSClipPagination];
    [info setVerticalPagination:NSClipPagination];
    [info setHorizontallyCentered:NO];
    [info setVerticallyCentered:NO];

    NSImageView *view = [[NSImageView alloc] init];
    [view setImage:image];
    [view setImageScaling:NSScaleProportionally];
    [view setBoundsOrigin:NSMakePoint(boundsX, boundsY)];
    [view setBoundsSize:NSMakeSize(boundsWidth, boundsHeight)];
    [view translateOriginToPoint:NSMakePoint(boundsX, [info paperSize].height -
                                                      boundsHeight - boundsY)];

    NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:view printInfo:info];
    [printOp setShowsPrintPanel:NO];
    [printOp runOperation];
}
那是无效的。您需要使用
initWithFrame:
初始化视图。您可能需要传递一个由
NSZeroPoint
和图像大小组成的帧


至于使用
setBoundsOrigin:
setBoundsSize:
:我不确定这些方法是否有效,假设你是想裁剪图像。您可以尝试它们(在解决上述问题后),但我觉得从旧图像的所需部分创建新图像更安全。要做到这一点,您可以创建一个所需大小的空图像,锁定焦点,在新图像的原点绘制旧图像的正确部分,解锁新图像的焦点,然后将新图像而不是旧图像提供给图像视图。

别忘了释放您已分配的内容。您使用的是哪个操作系统平台版本?无论你在哪里尝试它,它的行为都是一样的吗?@Quinn:Platform=Leopard。它在任何地方的表现都一样吗?未知:我只有自己的电脑可以尝试。是的,复制/粘贴工作不好--已修复。至于NSMakePoint,NSMakeSize()--是的,但这不是我的问题。我不想裁剪图像。我想打印整个图像。我将其更改为initWithFrame。它现在没有崩溃,但它启动了打印机应用程序,没有窗口,也没有文档——即使我点击预览。有什么想法吗?如果你想要整个图像,不需要设置边界。你所说的“它启动没有窗口和文档的打印机应用程序”是什么意思?@Hosey:应该发生的是预览应该启动并显示文档(在本例中为图像)的PDF预览;实际情况是,打印机应用程序(例如,如果我的打印机已配置为“HP 2700”),则会启动具有该名称的应用程序(您知道,显示该打印机打印队列的应用程序),但不会打印任何内容,因为没有生成文档。
NSImage *image = [[NSImage alloc] initWithContentsOfFile:spoolFile];
if ([image isValid]) {
    NSPrintInfo *info = [NSPrintInfo sharedPrintInfo];
    [info setHorizontalPagination:NSClipPagination];
    [info setVerticalPagination:NSClipPagination];
    [info setHorizontallyCentered:NO];
    [info setVerticallyCentered:NO];

    NSImageView *view = [[NSImageView alloc] init];
    [view setImage:image];
    [view setImageScaling:NSScaleProportionally];
    [view setBoundsOrigin:NSMakePoint(boundsX, boundsY)];
    [view setBoundsSize:NSMakeSize(boundsWidth, boundsHeight)];
    [view translateOriginToPoint:NSMakePoint(boundsX, [info paperSize].height -
                                                      boundsHeight - boundsY)];

    NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:view printInfo:info];
    [printOp setShowsPrintPanel:NO];
    [printOp runOperation];
}
NSImageView *view = [[NSImageView alloc] init];