我试图裁剪NSImage中含有PDF。当打印我使用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中是模糊的,并且不再适合于印刷。任何想法?

有帮助吗?

解决方案

lockFocus / unlockFocus执行光栅绘制到图像的缓存。这就是为什么它的“模糊” - 它的低分辨率,可能未对准。您需要矢量绘图。

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

其他提示

下面是执行什么彼得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]];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top