CGContextRef绘制模糊或模糊的图像

时间:2016-11-08 20:02:09

标签: objective-c core-graphics cgcontextref

我使用以下方法创建了新的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);

当我尝试使用此上下文绘制圆形或文本时,我会得到奇怪的模糊对象:

enter image description here

enter image description here

当我将CGContextSetShouldAntialias设置为true时,图像变得过于模糊:

enter image description here

enter image description here

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

感谢。

更新: 当我画直线或矩形时很好

1 个答案:

答案 0 :(得分:1)

  

我得到像这些奇怪的模糊对象

不,你不是。你得到一个位图。您必须执行某些操作 else 才能获得可以看到的对象 - 并且您没有显示您的操作。但问题不是整个问题吗?您必须将位图转换为UIImage,并且需要代码。如果以块状,像素化的方式进行,则会得到块状的像素化结果。

我使用了您的位图上下文代码并在其中绘制了一个实心圆圈,然后将位图上下文转换为UIImage并将该UIImage放入UIImageView中,它看起来像这样:

enter image description here

嗯, 看起来很顺利。所以你的位图上下文肯定不是问题所在,但你从那里得到的是 visible 这个问题。

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