使用CoreGraphics绘制视网膜显示 – 图像像素化

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

我正在绘制使用以下function:

- (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 } 

我发现的build议是使用scaleFactor属性或CGContextSetShouldAntialias()函数 ,但是这些都没有帮助到目前为止。 (虽然我可能使用不当。)

任何帮助将不胜感激。

您需要将UIGraphicsBeginImageContextreplace为

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

UIGraphicsBeginImageContextWithOptions是在4.x软件中引入的。 如果你打算在3.x设备上运行这个代码,你需要弱连接UIKit框架。 如果您的部署目标是4.x或更高,您可以使用UIGraphicsBeginImageContextWithOptions,而无需任何额外的检查。