NSImage - 裁剪后 PDF 模糊
我正在尝试裁剪包含 PDF 的 NSImage。 打印时,我使用 NSImage 的 drawInRect 让它只绘制我需要的内容 - 这效果很好。
但是,现在我尝试创建一个仅包含裁剪区域的新 NSImage。 我玩了一段时间,然后在 CocoaBuilder 上找到了这段代码:
- (NSImage *) imageFromRect: (NSRect) rect
{
NSAffineTransform * xform = [NSAffineTransform transform];
// translate reference frame to map rectangle 'rect' into first quadrant
[xform translateXBy: -rect.origin.x
yBy: -rect.origin.y];
NSSize canvas_size = [xform transformSize: rect.size];
NSImage * canvas = [[NSImage alloc] initWithSize: canvas_size];
[canvas lockFocus];
[xform concat];
// Get NSImageRep of image
NSImageRep * rep = [self bestRepresentationForDevice: nil];
[rep drawAtPoint: NSZeroPoint];
[canvas unlockFocus];
return [canvas autorelease];
}
这可以工作,但返回的 NSImage 是模糊的,不再适合打印。 有任何想法吗?
I'm trying to crop a NSImage which contains a PDF. When printing I am using NSImage's drawInRect to have it draw only what I need - and this works great.
But, now instead I'm trying to create a new NSImage of just the cropped area. I played with it for a while, then found this code on CocoaBuilder:
- (NSImage *) imageFromRect: (NSRect) rect
{
NSAffineTransform * xform = [NSAffineTransform transform];
// translate reference frame to map rectangle 'rect' into first quadrant
[xform translateXBy: -rect.origin.x
yBy: -rect.origin.y];
NSSize canvas_size = [xform transformSize: rect.size];
NSImage * canvas = [[NSImage alloc] initWithSize: canvas_size];
[canvas lockFocus];
[xform concat];
// Get NSImageRep of image
NSImageRep * rep = [self bestRepresentationForDevice: nil];
[rep drawAtPoint: NSZeroPoint];
[canvas unlockFocus];
return [canvas autorelease];
}
This works, but the returned NSImage is blurry, and no longer suitable for printing. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是执行 Peter Hosey 回答的代码。 谢谢!
Here is the code to perform what Peter Hosey answered. Thanks!
lockFocus
/unlockFocus
对图像的缓存执行光栅绘制。 这就是它“模糊”的原因——它的分辨率低,而且可能套准错误。 你需要矢量绘图。使用 PDF 套件。 首先,将每个页面的裁剪框设置为您的矩形。 然后,您应该能够从 PDFDocument 的
dataRepresentation
创建裁剪后的 NSImage。lockFocus
/unlockFocus
performs raster drawing to the image's cache. That's why it's “blurry”—it's low-resolution and possibly misregistered. You need vector drawing.Use PDF Kit. First, set the crop box of each page to your rectangle. You should then be able to create your cropped NSImage from the
dataRepresentation
of the PDFDocument.