使用CoreGraphics在视网膜显示器上绘图 - 图像像素化 [英] Drawing on the retina display using CoreGraphics - Image pixelated

查看:326
本文介绍了使用CoreGraphics在视网膜显示器上绘图 - 图像像素化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我使用以下函数绘图:

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

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.

推荐答案

你需要用

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

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

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.

这篇关于使用CoreGraphics在视网膜显示器上绘图 - 图像像素化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆