Hey everyone,
I'm trying to update an app I have which lets the user draw with their finger. I do this in Quartz. However, when I test on the iPhone 4 (well, the simulator, actually, as I don't have an iPhone 4), the Quartz drawing looks pixelated.
They way I'm drawing is as follows:
In my UIView subclass, in the initWithFrame method, I have this:
Code:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
_myContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8,
4*self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedLast);
drawingLayer = CGLayerCreateWithContext(_myContext, self.frame.size, NULL);
CGContextRef layerContext = CGLayerGetContext(drawingLayer);
CGContextSetLineWidth(layerContext, 10.0);
CGContextSetLineCap(layerContext, kCGLineCapRound);
drawingColor = [UIColor redColor];
CGContextSetStrokeColorWithColor(layerContext, drawingColor.CGColor);
Then, in the drawRect method, I have this:
Code:
ctx = UIGraphicsGetCurrentContext();
CGImageRef image = CGBitmapContextCreateImage(_myContext);
CGContextDrawImage(ctx, self.bounds, image);
CGImageRelease(image);
CGContextDrawLayerInRect(ctx, self.bounds, drawingLayer);
And finally, in touchesMoved, I have this:
Code:
CGContextSetLineWidth(layerContext, pointSize);
CGPoint pastLocation = [touch previousLocationInView:self];
CGContextBeginPath(layerContext);
CGContextMoveToPoint(layerContext, pastLocation.x, pastLocation.y);
CGContextAddLineToPoint(layerContext, location.x, location.y);
CGContextStrokePath(layerContext);
I believe the error is in this line
Code:
_myContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8,
4*self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedLast);
because if I change it to
Code:
_myContext = CGBitmapContextCreate(NULL, 640, 960, 8,
4*self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedLast);
everything looks sharp. However, when I do that, I get the
Code:
<Error>: CGBitmapContextCreateImage: invalid context 0x0
message.
Any help or suggestions would be greatly appreciated. Thanks!