【发布时间】:2013-01-28 19:45:51
【问题描述】:
我正在尝试使用 NSView 对象保存 PDF 文件。
这是Square 类(NSView 的子类)的实现。
@implementation Square
- (id)initWithColor:(NSColor *)aColor;
{
if (self = [super init])
{
self.frame = NSMakeRect(0, 0, 50, 50);
self.color = aColor;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[self.color set];
NSRectFill(dirtyRect);
}
这是我的DrawView 的部分实现-NSView 的子类
- (void)awakeFromNib
{
squareGroup = [[NSMutableArray alloc] init];
}
- (void)addSquare:(Square *)square
{
[squareGroup addObject:square];
[self addSubview:square];
}
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor whiteColor] set];
NSRectFill(dirtyRect);
}
我可以点击按钮Red 或Blue 并添加蓝色或红色的Square 对象,当我点击Save 时,我想保存白色DrawView 和Square 对象。我现在可以在DrawView 上移动Square 对象,因此每个Square 对象都位于不同的位置。
我的保存方法如下所示(在DrawView 类中):
- (void)saveAsPDF
{
NSString *homeDirectory = NSHomeDirectory();
NSURL *fileURL = [NSURL fileURLWithPath:[homeDirectory stringByAppendingPathComponent:@"file.pdf"]];
CGRect mediaBox = self.bounds;
CGContextRef ctx = CGPDFContextCreateWithURL((__bridge CFURLRef)(fileURL), &mediaBox, NULL);
CGPDFContextBeginPage(ctx, NULL);
for (Square *square in squareGroup) {
[square.layer renderInContext:ctx];
}
[self.layer renderInContext:ctx];
CGPDFContextEndPage(ctx);
CFRelease(ctx);
}
结果我的主目录中只有空白文件。
我的保存方法有什么问题?我怎样才能正确地做到这一点?
【问题讨论】:
标签: macos pdf cocoa draw quartz-graphics