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

在我的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,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 property,或CGContextSetShouldAntialias() function,但这两个都没有帮助到目前为止. (虽然我可能不正确地使用它们)

任何帮助将不胜感激.

解决方法

您需要用UIGraphicsBeginImageContext替换
if (UIGraphicsBeginImageContextWithOptions != NULL) {
  UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
} else {
  UIGraphicsBeginImageContext(size);
}

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...