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