【问题标题】:Drawing on the retina display using CoreGraphics - Image pixelated使用 CoreGraphics 在视网膜显示器上绘图 - 图像像素化
【发布时间】:2011-08-06 09:17:52
【问题描述】:

在我的 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,但到目前为止这些都没有帮助。 (虽然我可能使用不当。)

任何帮助将不胜感激。

【问题讨论】:

    标签: ios core-graphics retina-display


    【解决方案1】:

    您需要将 UIGraphicsBeginImageContext 替换为

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

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

    【讨论】:

    • 这是我在 Retina res 中捕获它所需要的 - 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多