views:

70

answers:

3

Hi there. I am not trying to draw on a component I am simply trying to create a new context (I think) and spit out a UIImage with the contents of my drawing on it. I'm not trying to draw on any existing component. I am using the following code:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, self.textSize.width, self.textSize.height, 8, 4 * self.textSize.width, 
                                             colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1);
NSString *text = @"Hello world";
[text drawAtPoint:CGPointMake(0, 0) forWidth:maxWidth withFont:font lineBreakMode:UILineBreakModeWordWrap];

CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

UIImage * myRendering = [UIImage imageWithCGImage:imageMasked];

Unfortunately I am getting an "invalid context" error message. Searching Google only seemed to bring up people trying to draw on existing components. I want to spit out a new UIImage.

I tried the example here for creating a bitmap context - http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-SW9

I still get:

: CGContextGetShouldSmoothFonts: invalid context : CGContextSetFont: invalid context : CGContextSetTextMatrix: invalid context : CGContextSetFontSize: invalid context etc...

A: 

You always draw against a context. The context itself can be linked to the Display, a Bitmap or even a PDF.

Depending on what you want to achieve you have to create a context or use an existing one. For custom components (I assumed you mean this because you mentioned a JPanel) you just override the drawRect method of an UIView like so.

- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();

As you can see the context was already created for you and you get it by calling UIGraphicsGetCurrentContext().

sliver
I just want to draw on a no context and return a UIImage of my drawings...can this be done?
Mike Simmons
So anyway copied the code here: http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-SW9can't see how I could have gotten it wrong - still get:Fri Jul 16 10:04:52 MitchMac.local Yearbook[75297] <Error>: CGContextGetShouldSmoothFonts: invalid context
Mike Simmons
A: 

Ok so the problem was I didn't put my bitmap drawing code in viewDidLoad. I really don't understand this stuff. Why is it so unintuitive?

Mike Simmons
A: 

First of all, check if you actually created a context by checking the return value of CGBitmapContextCreate. i.e.

if ( context == NULL ) NSLog(@"glad I always check my return values!");

And check the console for any message.

If you haven't created a context, check if you are developing on iOS 4.0 or later. If not, don't pass NULL as first argument but malloc a chuck of memory.

mvds
so ipad i3.2 would work with NULL right.
Mike Simmons
Who cares, just check return values and then debug. Not the other way around. (no I don't think `3.2 >= 4.0` but I'll check my math)
mvds