オブジェクトを含む PDF ファイルを保存しようとしていNSView
ます。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);
}
これは my DrawView
- sublcass の実装の一部です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
Square
DrawView
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);
}
その結果、ホームディレクトリに空のファイルしかありません。
保存方法の何が問題になっていますか? どうすれば正しくできますか?