开发者

my colour bitmap is only black and white in drawRect (iPhone)

I want to preserve previous drawing in drawRect (the system clears it down each time) and it seems that the way to go is to use a bitmap context and sure enough, this works except that now my colour is gone leaving only black and white instead.

Here is my "static" context

-(id)initWithCoder:(NSCoder *)aDecoder {
...
 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
 contextRef = CGBitmapContextCreate(NULL, 
            320, 
            640, 
            8, 
            320*4, 
          开发者_高级运维  colorSpace, 
            kCGImageAlphaPremultipliedLast);
...
}

and here is my replacement context:

-(void)drawRect:(CGRect)rect {
        myDrawRoutine();

 CGContextRef context = UIGraphicsGetCurrentContext();

 CGContextDrawImage(context, rect, CGBitmapContextCreateImage(contextRef));

}

And this is the draw routine's guts:

...
// CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextRef context = contextRef;
 if( !(context) )
  return;

 CGContextSetFillColor(context, CGColorGetComponents([co CGColor]));
 CGContextSetStrokeColor(context, CGColorGetComponents([co CGColor]));

 CGContextFillEllipseInRect(context, CGRectMake((*i)->x,
               (*i)->y, 
               (*i)->diam,
               (*i)->diam));
...
}


Thanks tonclon

That has made a hell of an increase in speed :)

But it still draws in black and white :(

Here is the above code + modifications...

Here is my "static context":

    -(id)initWithCoder:(NSCoder *)aDecoder {
    ...
    }
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    contextRef = CGBitmapContextCreate(NULL, 
                    320, 
                    640, 
                    8, 
                    320*4, 
                    colorSpace, 
                    kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colorSpace);

    cglayer = CGLayerCreateWithContext(contextRef, CGSizeMake(320.0f, 640.0f), NULL);
    contextRef = CGLayerGetContext(cglayer);
    ...
    }

and here is my "replacement context" i.e. the cglayer is used instead (the context is no longer replaced).

-(void)drawRect:(CGRect)rect {
    for(std::vector<Body*>::iterator i = bodyVec.begin(); i < bodyVec.end(); ++i)
        move(i);

    CGContextRef context = UIGraphicsGetCurrentContext();

//  CGContextDrawImage(context, rect, CGBitmapContextCreateImage(contextRef));
    CGContextDrawLayerInRect(context, rect, cglayer);

}

(The "draw routine's guts" remain the same.)


At last! Thanks to this thread Extracting rgb from UIColor

I got the answer - use CGContextSetRGBFillColor instead and then feed it with the RGB from CGColorGetComponents indirectly.

CGColorRef color = [co CGColor];    
int numComponents = CGColorGetNumberOfComponents(color);
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat alpha;
if (numComponents == 4)
{
    const CGFloat *components = CGColorGetComponents(color);
    red = components[0];
    green = components[1];
    blue = components[2];
    alpha = components[3];
}

//  CGContextSetFillColor(context, CGColorGetComponents([co CGColor]));
//  CGContextSetStrokeColor(context, CGColorGetComponents([co CGColor]));
CGContextSetRGBFillColor(context, red, green, blue, alpha);
CGContextSetRGBStrokeColor(context, red, green, blue, alpha);

CGContextFillEllipseInRect(context, CGRectMake((*i)->x,
                                               (*i)->y, 
                                               (*i)->diam,
                                               (*i)->diam));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜