CGContextRef 绘制模糊或模糊的图像

CGContextRef draws fuzzy or blurred images

我使用以下方法创建了新的 CGContext:

        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 设置为 true 时,图像变得过于模糊:

我希望文字和图像变成像通常的标签或视图(不模糊但不模糊)。我只能使用这种方法来获取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;

谢谢。

更新: 当我只画直线或矩形时没问题

I get weird fuzzy objects like these

不,你不知道。你得到一个位图。你必须做一些事情 else 才能得到一个你可以 see 的对象——你没有展示你所做的是什么。但这不是全部问题吗?您必须将位图转换为 UIImage,这需要 code。如果你以块状、像素化的方式进行,你将得到块状、像素化的结果。

我用了你的位图上下文代码,在里面画了一个实心圆,然后把位图上下文变成了一个UIImage,然后把那个UIImage放到了一个UIImageView中,它看起来是这样的:

嗯,好像还算顺利。所以问题肯定不是你的位图上下文,而是你如何从那里得到一些可见的问题。

但是,就我个人而言,我不明白你为什么要这么麻烦。如果您的目标是生成要在界面中显示的图像,为什么不像其他人一样使用 UIGraphicsBeginImageContextWithOptions