禁用UIBezierPath的抗锯齿功能 [英] disable anti-aliasing for UIBezierPath

查看:691
本文介绍了禁用UIBezierPath的抗锯齿功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在没有消除锯齿的情况下渲染UIBezierPaths,然后将它们保存为PNG以保留完整的像素表示(例如,不要让JPEG捣乱图像)。我在尝试触摸UIBezierPaths之前尝试调用下面的CG函数,但似乎没有对生成的渲染图像产生任何影响。仍然使用抗锯齿(即平滑)渲染路径。

I need to render UIBezierPaths without anti-aliasing and then save them as PNG to retain the full pixel representations (for example, not let JPEG muck the image up). I've tried calling the CG functions below just before stroking the UIBezierPaths, but it seems none have any effect on the resultant rendered image. The paths are still rendered with anti-aliasing (i.e. smoothed).

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

任何点击都会非常感激。

Any hits would be greatly appreciated.

推荐答案

当我使用这些选项时,它会关闭抗锯齿功能。左侧是默认选项。在右边,有你的选择。

When I use those options, it turns off antialiasing. On the left is with the default options. On the right, with your options.

如果你这么做很容易控制重新使用 UIView 子类。这是我的 drawRect

This is easy to control if you're using a UIView subclass. This is my 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 ,那我不认为你可以在屏幕上控制抗锯齿,因为正如文档所说

If you're using a CAShapeLayer, then I don't think you can control the antialiasing on screen, because as the documentation says:


将绘制抗锯齿形状,并且只要有可能,它会在光栅化之前映射到屏幕空间以保持分辨率独立性。但是,应用于图层或其祖先的某些类型的图像处理操作(如CoreImage滤镜)可能会强制在本地坐标空间中进行光栅化。

The shape will be drawn antialiased, and whenever possible it will be mapped into screen space before being rasterized to preserve resolution independence. However, certain kinds of image processing operations, such as CoreImage filters, applied to the layer or its ancestors may force rasterization in a local coordinate space.

但是,无论屏幕上的抗锯齿如何,如果您想让屏幕快照不被抗锯齿,您可以将 CGContextSetShouldAntialias 插入 captureScreen 例程:

But, regardless of the antialiasing on screen, if you want to have your snapshot of the screen not be antialiased, you can insert your CGContextSetShouldAntialias into the captureScreen routine:

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

这篇关于禁用UIBezierPath的抗锯齿功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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