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

9
在我的iOS应用程序中,我试图使用CoreGraphics绘制曲线。绘图本身工作正常,但在Retina显示屏上,图像使用相同的分辨率绘制,没有被像素加倍。结果是一个像素化的图像。 我正在使用以下函数进行绘制:
- (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
}

我找到的建议是使用scaleFactor属性CGContextSetShouldAntialias()函数,但迄今为止这两者都没有帮助。(尽管我可能使用不当。)
非常感谢任何帮助。
1个回答

31

您需要将 UIGraphicsBeginImageContext 替换为

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

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


这就是我需要的以Retina分辨率捕捉它的方式 - 谢谢! - Gaurav Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接