2010-11-23 19 views
7

私はCGImageRefを作成し、それにポイントを描画します。CGImageRefに描画

空のCGImageRefを作成して描画できるコンテキスト。 CGContextRefまたはCGBitmapContextRef?

空のCGImageRefイメージを作成して描画するコードを提供できるなら、私は感謝します。

答えて

19
#define HIRESDEVICE (((int)rintf([[[UIScreen mainScreen] currentMode] size].width/[[UIScreen mainScreen] bounds].size.width)>1)) 


- (CGImageRef) blerg 
    { 
    CGFloat imageScale = (CGFloat)1.0; 
    CGFloat width = (CGFloat)180.0; 
    CGFloat height = (CGFloat)180.0; 

    if (HIRESDEVICE) 
     { 
     imageScale = (CGFloat)2.0; 
     } 

    // Create a bitmap graphics context of the given size 
    // 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(NULL, width * imageScale, height * imageScale, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); 


    // Draw ... 
    // 
    CGContextSetRGBFillColor(context, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)1.0); 
    // … 


    // Get your image 
    // 
    CGImageRef cgImage = CGBitmapContextCreateImage(context); 

    CGColorSpaceRelease(colorSpace); 
    CGContextRelease(context); 

    return cgImage; 
    } 
+0

ありがとうございました。 – Cyprian

+2

何らかの理由で、塗りつぶしの色の値を何に設定しても、私はこれを試したときに常に空白の灰色の領域がありました。 – micahhoover

+3

iOS4以降では 'UIGraphicsBeginImageContextWithOptions'を使用します。そうすれば、スケールファクタを考慮する必要はありません。 – bioffe