NSImage - 裁剪后 PDF 模糊

发布于 2024-07-12 19:41:54 字数 848 浏览 10 评论 0原文

我正在尝试裁剪包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦与时光遇 2024-07-19 19:41:54

这是执行 Peter Hosey 回答的代码。 谢谢!

PDFDocument *thePDF = [[PDFDocument alloc] initWithData:pdfData];
PDFPage *thePage = [thePDF pageAtIndex:0];
NSRect pageCropRect = NSMakeRect(0, 100, 100, 100);

[thePage setBounds:pageCropRect forBox:kPDFDisplayBoxMediaBox];
NSImage *theCroppedImage = [[NSImage alloc] initWithData:[thePage dataRepresentation]];

Here is the code to perform what Peter Hosey answered. Thanks!

PDFDocument *thePDF = [[PDFDocument alloc] initWithData:pdfData];
PDFPage *thePage = [thePDF pageAtIndex:0];
NSRect pageCropRect = NSMakeRect(0, 100, 100, 100);

[thePage setBounds:pageCropRect forBox:kPDFDisplayBoxMediaBox];
NSImage *theCroppedImage = [[NSImage alloc] initWithData:[thePage dataRepresentation]];
燕归巢 2024-07-19 19:41:54

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文