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

发布于 2024-11-28 03:01:30 字数 1386 浏览 2 评论 0原文

在我的 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
}

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

任何帮助将不胜感激。

In my iOS application, I am trying to draw curves using CoreGraphics. The drawing itself works fine, but on the retina display the image gets drawn using the same resolution, and does not get pixel doubled. The result is a pixelated image.

I am drawing using the following functions:

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

The advice I have found was to use the scaleFactor property, or the CGContextSetShouldAntialias() function, but neither of these helped so far. (Although I might have used them improperly.)

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼角的笑意。 2024-12-05 03:01:30

您需要将 UIGraphicsBeginImageContext 替换为

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

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

You need to replace UIGraphicsBeginImageContext with

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

UIGraphicsBeginImageContextWithOptions was introduced in 4.x software. If you're going to run this code on 3.x devices, you need to weak link UIKit framework. If your deployment target is 4.x or higher, you can just use UIGraphicsBeginImageContextWithOptions without any additional checking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文