I created new CGContext using:
NSUInteger width = newRect.size.width;
NSUInteger height = newRect.size.height;
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
UInt32 * pixels;
pixels = (UInt32 *) calloc(height * width, sizeof(UInt32));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGContextSetShouldAntialias(self.graphContext, false);
CGContextSetInterpolationQuality(self.graphContext, kCGInterpolationHigh);
When I trying to draw circle or text using this context I get weird fuzzy objects like these:
When I set CGContextSetShouldAntialias to true images becomes too blured:
I want text and images becomes such as usual label or views (not blurred but not fuzzy). I can use only this approach to obtain CGContextRef.
My code for drawing text:
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect );
NSAttributedString* attString = [[NSAttributedString alloc]
initWithString:text
attributes:self.attributes];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, [attString length]), path, NULL);
CTFrameDraw(frame, self.graphContext);
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);
Code for drawing circles:
CGRect minPath = CGRectMake(xPoint - extremaPointWidth / 2, minY - extremaPointWidth /2, extremaPointWidth, extremaPointWidth);
CGContextAddPath(self.graphContext, createRoundedCornerPath(minPath, cornerRadius));
CGContextFillPath(self.graphContext);
Creating rounding path
// create a mutable path
CGMutablePathRef path = CGPathCreateMutable();
// get the 4 corners of the rect
CGPoint topLeft = CGPointMake(rect.origin.x, rect.origin.y);
CGPoint topRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y);
CGPoint bottomRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
CGPoint bottomLeft = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
CGPathMoveToPoint(path, NULL, topLeft.x + cornerRadius, topLeft.y);
CGPathAddLineToPoint(path, NULL, topRight.x - cornerRadius, topRight.y);
CGPathAddQuadCurveToPoint(path, NULL, topRight.x, topRight.y, topRight.x, topRight.y + cornerRadius);
CGPathAddLineToPoint(path, NULL, bottomRight.x, bottomRight.y - cornerRadius);
CGPathAddQuadCurveToPoint(path, NULL, bottomRight.x, bottomRight.y, bottomRight.x - cornerRadius, bottomRight.y);
CGPathAddLineToPoint(path, NULL, bottomLeft.x + cornerRadius, bottomLeft.y);
CGPathAddQuadCurveToPoint(path, NULL, bottomLeft.x, bottomLeft.y, bottomLeft.x, bottomLeft.y - cornerRadius);
CGPathAddLineToPoint(path, NULL, topLeft.x, topLeft.y + cornerRadius);
CGPathAddQuadCurveToPoint(path, NULL, topLeft.x, topLeft.y, topLeft.x + cornerRadius, topLeft.y);
return path;
Thanks.
UPDATE: When I draw just straight line or rectangle it's fine
I get weird fuzzy objects like these
No, you don't. You get a bitmap. You have to do something else in order to get an object that you can see — and you have not shown what it is that you do. But isn't that the whole issue? You have to turn a bitmap into a UIImage, and that takes code. If you do it in a blocky, pixellated way, you will get a blocky, pixellated result.
I used your bitmap context code and drew a filled circle into it, and then turned the bitmap context into a UIImage and put that UIImage in a UIImageView, and it looks like this:
Well, that seems smooth enough. So surely it isn't your bitmap context that's the problem, but how you are getting from there to something visible that's the problem.
However, personally I don't understand why you're going to all this trouble. If your goal is to generate an image you are going to display in the interface, why don't you just use UIGraphicsBeginImageContextWithOptions
like everyone else?