0

UIViewint の配列をinにブリットする方法の例をたくさん見つけましdrawRectたが、最も単純なものはまだ私を困惑させています。これは問題なく動作しますが、まだ 3 つの質問があります。

~ なぜ 2 つのコンテキストがあるのですか?

〜なぜpush/pop文脈?

〜コピーを回避できますか?(アップルのドキュメントによると、CGBitmapContextCreateImage はメモリ ブロックをコピーします)

- (void)drawRect:(CGRect)rect {
    CGColorSpaceRef color = CGColorSpaceCreateDeviceRGB();
    int PIX[9] = {  0xff00ffff,0xff0000ff,0xff00ff00,
                        0xff0000ff,0xff00ffff,0xff0000ff,
                        0xff00ff00,0xff0000ff,0xff00ffff};
    CGContextRef context = CGBitmapContextCreate((void*)PIX,3,3,8,4*3,color,kCGImageAlphaPremultipliedLast);
    UIGraphicsPushContext(context);
    CGImageRef image = CGBitmapContextCreateImage(context);
    UIGraphicsPopContext();
    CGContextRelease(context);
    CGColorSpaceRelease(color);
    CGContextRef c=UIGraphicsGetCurrentContext();
    CGContextDrawImage(c, CGRectMake(0, 0, 10, 10), image);
    CGImageRelease(image);
}
4

1 に答える 1

0

メソッドは、配列を 3x3 サイズの画像に描画し、次にその画像を現在のコンテキストで 10x10 サイズに描画します。この場合は、あなたUIViewCALayer.

UIGraphicsPushContext現在描画してCGContextいるを設定できます。したがって、最初の呼び出しの前に、 currentは であり、次に、描画されるイメージであるnew をプッシュします。CGContextUIViewCGContext

この呼び出しにより、のコンテキスト ( ) がUIGraphicsPopContext復元されます。次に、そのコンテキストへの参照を取得し、次の行を使用して、作成されたイメージをそれに描画します。UIView

CGContextDrawImage(c, CGRectMake(0, 0, 10, 10), image);

コピー操作を回避する限り、ドキュメントによると、書き込み時にコピーされることもありますが、これらの条件がいつ発生するかは指定されていません。

The CGImage object returned by this function is created by a copy operation. Subsequent changes to the bitmap graphics context do not affect the contents of the returned image. In some cases the copy operation actually follows copy-on-write semantics, so that the actual physical copy of the bits occur only if the underlying data in the bitmap graphics context is modified. As a consequence, you may want to use the resulting image and release it before you perform additional drawing into the bitmap graphics context. In this way, you can avoid the actual physical copy of the data.

于 2013-03-25T04:38:15.430 に答える