я создал новый CGContext с помощью:CGContextRef рисует нечеткие или размытые изображения
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);
Когда я пытаюсь нарисовать круг или текст, используя этот контекст я получаю странные нечеткие объекты, подобные этим:
Когда я установил CGContextSetShouldAntialias на истинные изображения, становится слишком размытым:
Я хочу, чтобы текст и изображения становятся такими, как обычно, метка или взгляды (не размытые, но не нечеткие). Я могу использовать только этот подход для получения CGContextRef.
Мой код для рисования текста:
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);
Код для рисования кругов:
CGRect minPath = CGRectMake(xPoint - extremaPointWidth/2, minY - extremaPointWidth /2, extremaPointWidth, extremaPointWidth);
CGContextAddPath(self.graphContext, createRoundedCornerPath(minPath, cornerRadius));
CGContextFillPath(self.graphContext);
Создание закругление пути
// 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;
Спасибо.
UPDATE: Когда я рисую только прямую линию или прямоугольник, это хорошо
Спасибо за ваш ответ. Так что, может быть, правильный вопрос: как правильно получить изображение? Я использую следующие строки: CGImageRef cgImage = CGBitmapContextCreateImage (self.graphContext); UIImage * point = [UIImage imageWithCGImage: cgImage]; – Artem
Правильно, поэтому вы используете графический контекст scale = 1 в мире scale = 2. Если, как я предложил в своем ответе, вы используете 'UIGraphicsBeginImageContextWithOptions' со шкалой 0, эта проблема будет позаботиться о вас. – matt
Но как я могу исправить эту проблему в моем случае? Я действительно попытался использовать ваше предложение, прежде чем я столкнулся с растровым изображением и вам нужно только растровое изображение. В моем случае мне нужно нарисовать тысячи кругов, а UIGraphicsBeginImageContextWithOptions вызовет у меня некоторые проблемы – Artem