首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印NSImage

打印NSImage
EN

Stack Overflow用户
提问于 2009-09-16 16:49:09
回答 1查看 2.7K关注 0票数 2

我将Cocoa代码迁移到Cocoa+ JNI,因为Cocoa是不推荐的。代码打印存储在文件中的图像。新的可可编码基本上是:

代码语言:javascript
复制
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];
}

运行此代码时,最终会发生以下情况:

代码语言:javascript
复制
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

为什么?如何简单地打印存储在文件中的图像?

EN

回答 1

Stack Overflow用户

发布于 2009-09-16 20:25:41

NSImageView *view = [NSImageView alloc init];

那是无效的。您需要使用initWithFrame:来初始化视图。您可能希望传递一个由NSZeroPoint和图像大小组成的帧。

至于setBoundsOrigin:setBoundsSize:的使用:假设您打算裁剪图像,我不确定这些操作是否有效。您可以尝试它们(在解决了上面的问题之后),但是我会觉得从旧的部分创建一个新的映像更安全。要做到这一点,您可以创建一个所需大小的空图像,锁定焦点,在新图像的原点绘制旧图像的正确部分,并在新图像上解锁焦点,然后将新图像而不是旧图像赋予图像视图。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1434175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档