使用CoreGraphics在视网膜显示器上绘图 - 图像像素化

时间:2011-08-06 09:17:53

标签: ios core-graphics retina-display

在我的iOS应用程序中,我正在尝试使用CoreGraphics绘制曲线。绘图本身工作正常,但在视网膜显示器上,图像使用相同的分辨率绘制,并且不会使像素加倍。结果是像素化图像。

我正在使用以下功能绘图:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.canvasView];

    UIGraphicsBeginImageContext(self.canvasView.frame.size);
    [canvasView.image drawInRect:self.canvasView.frame];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetShouldAntialias(ctx, YES);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(ctx, currentPoint.x, currentPoint.y);
    CGContextStrokePath(ctx);
    canvasView.image =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
    // some code omitted from this example
}

我发现的建议是use the scaleFactor propertyCGContextSetShouldAntialias() function,但到目前为止这些都没有帮助。 (虽然我可能不恰当地使用它们。)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:28)

您需要用

替换UIGraphicsBeginImageContext
if (UIGraphicsBeginImageContextWithOptions != NULL) {
  UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
} else {
  UIGraphicsBeginImageContext(size);
}

UIGraphicsBeginImageContextWithOptions在4.x软件中引入。如果您要在3.x设备上运行此代码,则需要弱链接UIKit框架。如果您的部署目标是4.x或更高,您可以使用UIGraphicsBeginImageContextWithOptions而无需任何其他检查。

相关问题