tags:

views:

428

answers:

5

Something with colorspace ? Note; if I draw on the current context directly it turns black; on an bitmap context it turns red.

Example:

CGContextRef context;// = UIGraphicsGetCurrentContext(); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
void *bitmapData = malloc(getWidth()*4*getHeight());
memset(bitmapData, 0, getWidth()*4*getHeight()); 
context = CGBitmapContextCreate (bitmapData, getWidth(), getHeight(), 8, 
        getWidth()*4, colorSpace, kCGImageAlphaPremultipliedFirst); 

CGContextSetStrokeColorWithColor(context, [blue CGColor]);
CGContextMoveToPoint(context, 0.0,0.0 ); 
CGContextAddLineToPoint(context, 480.0,300.0); 
CGContextStrokePath(context);

CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bitmapData, getWidth()*4*getHeight(), NULL);
CGColorRenderingIntent intent = kCGRenderingIntentDefault;
CGImageRef image2 = CGImageCreate(getWidth(), getHeight(), 8, 32, getWidth()*4, colorSpace, bitmapInfo,
           provider, NULL, NO, intent); 

CGContextDrawImage(UIGraphicsGetCurrentContext(),rect, image2);

Edit (day later); Ok, I restarted ( = made a new project in XCode, copy/pasted the code from this example) the entire project and then it is working. Anyone any idea how that is possible at all?

A: 

Try:

[[UIColor blueColor] set];

before you stroke.

jbrennan
Tried, didn't work. Thanks for the attempt though!
CharlesS
before you stroke ^.^
Mark
Like; [[UIColor blueColor] set]; CGContextStrokePath(context);?What does that actually do?
CharlesS
A: 

I've seen this. I'll bet your problem doesn't reproduce in the simulator.

This is an iPhone endian bug.

See qa1509 and use the code from listing #2.

Rhythmic Fistman
Thanks, but as far as I see I am using quite the same params :( And it actually *does* reproduce in the simulator.
CharlesS
A: 

Try changing kCGBitmapByteOrderDefault to kCGBitmapByteOrder32Big, or failing that kCGBitmapByteOrder32Little.

Rhythmic Fistman
That did something; now every color shows as black :)
CharlesS
A: 

Try erasing the context first. Either with memset(mem, -1, nbytes) or CG functions.

Rhythmic Fistman
Sorry, but how would I do that?
CharlesS
Ok, tried that too; same result.
CharlesS
Why don't you update the original code snippet, that was a bug.
Rhythmic Fistman
You mean; not doing memset was a bug? None of the examples has that (also from Apple)
CharlesS
Your pixels start out undefined, so you need to either memset the mem or CGContextFillRect the context.
Rhythmic Fistman
Thanks! No idea why the examples don't mention that.
CharlesS
I'm sure they do, but your code looks like it's derived from code that didn't need to: your context was originally the current context, which DID have defined pixels.
Rhythmic Fistman
A: 

I don't know where you got 'blue' from. Try making blue in place:

CGColorRef myBlue = CGColorCreateGenericRGB(0,0,1,1);
CGContextSetStrokeColorWithColor(context, myBlue);
Alex Brown
blue was created like this ; blue = [UIColor blueColor]; With your code I get a warning that CGColorCreateGenericRGB is deprecated then an error that CGColorCreateGenericRGB is not available.
CharlesS
Tried this; float black1[4] = {0.0, 0.0, 0.0, 1.0}; CGColorRef myBlue = CGColorCreate(colorSpace, black1); Still getting only red...
CharlesS