iPhoneで、オフスクリーンに描画して、表示する

ついに、できました。
オフスクリーンに、ドットを描画して、それを、ウインドウに描画することで表示されました。(^^;

ビットマップを作成して、コンテキストを返す(オフスクリーン)

CGContextRef createBitmapContext ( int pixelsWide,int pixelsHigh )
{
CGContextRef      bitmapContext=NULL;
void              *bitmapData;
CGColorSpaceRef   colorSpace;
	
if( bitmapData=calloc( 1,pixelsWide*pixelsHigh*4 ) )
		//  画像用メモリ確保
   {
    colorSpace=CGColorSpaceCreateDeviceRGB();  //  カラースペースを設定
    bitmapContext=CGBitmapContextCreate( bitmapData,pixelsWide,
     pixelsHigh,8,pixelsWide*4,colorSpace,kCGImageAlphaPremultipliedLast );

if( ! bitmapContext )    //  オフスクリーン用CGContextRef作成
	free( bitmapData );  //  失敗した場合にはメモリを解放する
	CGColorSpaceRelease( colorSpace );
 }
return bitmapContext;
}


描画ルーチン

- (void)drawRect:(CGRect)rect {
// 描画先のコンテキストの取得
CGContextRef cgContext =UIGraphicsGetCurrentContext();

// ビットマップコンテキスト作成(オフスクリーン)
CGContextRef bmp_context = createBitmapContext(300,300);

// コンテキストの ピクセルを取得
unsigned char * imageData = CGBitmapContextGetData (bmp_context);
	
// オフスクリーンに、ランダムにドットを打つ
for(int y=0; y<640; y++)
    for(int x=0; x<320; x++)
	*(imageData +y*320+x) = rand() & 0xff;

// ビットマップコンテキストから、イメージを取得
CGImageRef image = CGBitmapContextCreateImage(bmp_context);
// イメージを、ウインドウに転送
CGContextDrawImage(cgContext , CGRectMake(0,0,200,200),image );
}