1

オブジェクトを含む 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追加できます。クリックすると、オブジェクトを含む白を保存したいと思います。オブジェクトを移動できるようになったので、すべてのオブジェクトが別の場所に配置されます。SquareSaveDrawViewSquareSquareDrawViewSquare

私の保存方法は以下のようになります(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);
}

その結果、ホームディレクトリに空のファイルしかありません。

保存方法の何が問題になっていますか? どうすれば正しくできますか?

4

1 に答える 1

2

あなたを助けるかもしれない非常に役立つチュートリアルがここにあります

彼らは 2 つの方法を提案しています。1 つ目はこれですが、2 つ目はより包括的で一見の価値があります。

(void)didEnd:(NSSavePanel *)sheet
returnCode:(int)code
saveFormat:(void *)saveType
{
if (code == NSOKButton)
{
    if (pageIt)
    {

    }
    else
    {
        NSRect r = [textView bounds];
        NSData *data = [textView dataWithPDFInsideRect:r];

        [data writeToFile:[sheet filename] atomically:YES];
    }
}
}
于 2013-01-28T19:58:06.083 に答える