在我的 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财产 https://stackoverflow.com/questions/4707465/coregraphics-for-retina-display, 或者CGContextSetShouldAntialias()功能 https://stackoverflow.com/questions/5291510/drawing-with-core-graphics-looks-chunky-on-retina-display,但到目前为止这些都没有帮助。 (尽管我可能使用不当。)
任何帮助将不胜感激。
您需要将 UIGraphicsBeginImageContext 替换为
if (UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
} else {
UIGraphicsBeginImageContext(size);
}
UIGraphicsBeginImageContextWithOptions 是在 4.x 软件中引入的。如果您要在 3.x 设备上运行此代码,您需要弱链接 UIKit 框架。如果您的部署目标是 4.x 或更高版本,则只需使用 UIGraphicsBeginImageContextWithOptions 即可,无需任何其他检查。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)