【问题标题】:NSImage - after crop PDF is blurryNSImage - 裁剪后 PDF 模糊
【发布时间】:2009-02-06 23:18:11
【问题描述】:

我正在尝试裁剪包含 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 模糊,不再适合打印。有什么想法吗?

【问题讨论】:

    标签: cocoa pdfkit


    【解决方案1】:

    lockFocus/unlockFocus 对图像缓存执行光栅绘制。这就是它“模糊”的原因——它的分辨率低并且可能配准错误。你需要矢量图。

    使用 PDF 套件。首先,将每个页面的裁剪框设置为您的矩形。然后,您应该能够从 PDFDocument 的 dataRepresentation 创建裁剪的 NSImage。

    【讨论】:

      【解决方案2】:

      这是执行 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]];
      

      【讨论】:

      • 确保您及时发布或自动发布 PDF 和裁剪图像。
      • 顺便说一下,设置媒体框,而不是裁剪框。
      猜你喜欢
      • 2014-02-04
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      相关资源
      最近更新 更多