禁用UIBezierPath的消除锯齿

我需要呈现没有抗锯齿的UIBezierPaths,然后将它们保存为PNG,以保留完整的像素表示(例如,不让JPEG将图像弄糟)。 我已经尝试调用UIBezierPaths之前下面的CG函数,但似乎没有任何影响最终的渲染图像。 path仍然呈现抗锯齿(即平滑)。

CGContextSetShouldAntialias(c, NO); CGContextSetAllowsAntialiasing(c, NO); CGContextSetInterpolationQuality(c, kCGInterpolationNone); 

任何命中将不胜感激。

当我使用这些选项时,会closures抗锯齿function。 左边是默认选项。 在右边,与您的select。

在这里输入图像说明

如果你使用UIView子类,这很容易控制。 这是我的drawRect

 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetShouldAntialias(context, NO); [[UIColor redColor] setStroke]; UIBezierPath *path = [self myPath]; [path stroke]; } 

并捕获屏幕,从如何以编程方式截图

 - (void)captureScreen { if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale); else UIGraphicsBeginImageContext(self.window.bounds.size); [self.window.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData *data = UIImagePNGRepresentation(image); [data writeToFile:[self screenShotFilename] atomically:YES]; } 

如果你使用的是CAShapeLayer ,那么我不认为你可以控制屏幕上的抗锯齿,因为正如文档所说 :

该形状将被绘制反锯齿,并且在可能的情况下将被映射到屏幕空间,然后进行光栅化以保持分辨率的独立性。 但是,应用于图层或其祖先的某些image processing操作(如CoreImage滤镜)可能会强制在本地坐标空间中进行光栅化。

但是,不pipe屏幕上的抗锯齿,如果你想让屏幕的快照不被反锯齿,你可以将你的CGContextSetShouldAntialias插入到captureScreen例程中:

 - (void)captureScreen { if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale); else UIGraphicsBeginImageContext(self.window.bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetShouldAntialias(context, NO); [self.window.layer renderInContext:context]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData * data = UIImagePNGRepresentation(image); [data writeToFile:[self screenShotFilename] atomically:YES]; } 

你从哪里得到c ? 你确定c在绘图循环中引用与UIGraphicsGetCurrentContext()相同的东西,你使用[UIBezierPath stroke] ? 从上面的例子很难说。

如果您想确定您正在绘制到您正在configuration的相同上下文,请从UIBezierPath获取UIBezierPath ,然后直接绘制它:

 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicGetCurrentContext(); CGPathRef path = [self.bezier CGPath]; CGContextSetShouldAntialias(context, NO); CGContextAddPath(context, path); CGContextStrokePath(context); }