質問

のための私の願いをイメージは、特定のビューおよびそのすべてのsubviews.

このために私の作成コンテキストのロッドやルアーを使用することが、ビットマップと同じサイズのビューしていると思うか不明な描き方は、ビュー階層です。私使い分けることができサマリを単一ページです設定のコンテキストを明示的に呼び出しdrawRectものでなければならないすべてのsubviews.

見えないことに気づいたものNSViewインターフェイスできるこの疑いがあるのです。

役に立ちましたか?

解決

これらの書面のコードを自分のための最良の方法:

  • 対応可能性透明性の問題(一部、その他のオプションな白背景に全体画像)
  • 能楽に過ごすことができたよう

以下のコードは完全というわけではありませんのででものでなければならないスケーリング問題から境界をフレームなのisFlipped状態に非常にうです。ご注意くだけを描きsubviewsのsubsubviews,...再帰的に)ができるのではないかと考えても描画自体は非常に簡単ですぐ [self drawRect:[self bounds]] の実施 imageWithSubviews.

- (void)drawSubviews
{
    BOOL flipped = [self isFlipped];

    for ( NSView *subview in [self subviews] ) {

        // changes the coordinate system so that the local coordinates of the subview (bounds) become the coordinates of the superview (frame)
        // the transform assumes bounds and frame have the same size, and bounds origin is (0,0)
        // handling of 'isFlipped' also probably unreliable
        NSAffineTransform *transform = [NSAffineTransform transform];
        if ( flipped ) {
            [transform translateXBy:subview.frame.origin.x yBy:NSMaxY(subview.frame)];
            [transform scaleXBy:+1.0 yBy:-1.0];
        } else
            [transform translateXBy:subview.frame.origin.x yBy:subview.frame.origin.y];
        [transform concat];

        // recursively draw the subview and sub-subviews
        [subview drawRect:[subview bounds]];
        [subview drawSubviews];

        // reset the transform to get back a clean graphic contexts for the rest of the drawing
        [transform invert];
        [transform concat];
    }
}

- (NSImage *)imageWithSubviews
{
    NSImage *image = [[[NSImage alloc] initWithSize:[self bounds].size] autorelease];
    [image lockFocus];
    // it seems NSImage cannot use flipped coordinates the way NSView does (the method 'setFlipped:' does not seem to help)
    // Use instead an NSAffineTransform
    if ( [self isFlipped] ) {
        NSAffineTransform *transform = [NSAffineTransform transform];
        [transform translateXBy:0 yBy:NSMaxY(self.bounds)];
        [transform scaleXBy:+1.0 yBy:-1.0];
        [transform concat];
    }
    [self drawSubviews];
    [image unlockFocus];
    return image;
}

他のヒント

利用できる -[NSView dataWithPDFInsideRect:] 描画のための階層に見ら送信するには、PDFとして返される NSData オブジェクトです。きしていることなどを描画するのでビットマップ.

Are you sure you wantビットマップ表現でき連れて現れたからね(笑)。戦後、PDFき(少なくとも理論分解能-独立しています。

利用できる -[NSBitmapImageRep initWithFocusedViewRect:] 後にロックを中心にして、ビューの描画そのもの(およびそのsubviews)指定された矩形を塗りつぶします。

したいことは明示的です。る"の項をご参照くださいNSView図面のリダイレクトAPI"の10.4AppKitリリースします。

ることNSBitmapImageRepのためのキャッシュおよび確認すると:

NSGraphicsContext *bitmapGraphicsContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:cacheBitmapImageRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:bitmapGraphicsContext];
[[NSColor clearColor] set];
NSRectFill(NSMakeRect(0, 0, [cacheBitmapImageRep size].width, [cacheBitmapImageRep size].height));
[NSGraphicsContext restoreGraphicsState];

キャッシュす:

-[NSView cacheDisplayInRect:toBitmapImageRep:]

したい場合はより一般的に描画定の文脈の取り扱いビューの再帰性と透明性を正確

-[NSView displayRectIgnoringOpacity:inContext:]

ええ、この特定の使用は国連スケール画像データは何をするか、必要な

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top