drawRect中抗锯齿

技术杂谈60

在开始之前,我们需要创建一个DrawRectView

drawRect中抗锯齿

其初始代码为

//
//  DrawRectView.h
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.

//  Copyright © 2017年 TechCode. All rights reserved.

//

#import

@interface DrawRectView : UIView

@end
//
//  DrawRectView.m
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.

//  Copyright © 2017年 TechCode. All rights reserved.

//

#import "DrawRectView.h"

@implementation DrawRectView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        self.backgroundColor   = [UIColor clearColor];
        self.layer.borderWidth = 0.5f;
        self.layer.borderColor = [UIColor redColor].CGColor;
    }

    return self;
}

@end

在ViewController中使用(尺寸为100x100并居中)

drawRect中抗锯齿

显示效果如下(用红色边框显示边界)

drawRect中抗锯齿

修改DrawRectView.m代码如下

//
//  DrawRectView.m
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.

//  Copyright © 2017年 TechCode. All rights reserved.

//

#import "DrawRectView.h"

@implementation DrawRectView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        self.backgroundColor   = [UIColor clearColor];
        self.layer.borderWidth = 0.5f;
        self.layer.borderColor = [UIColor redColor].CGColor;
    }

    return self;
}

- (void)drawRect:(CGRect)rect {

    // Set stroke color
    [[UIColor blackColor] setStroke];

    // Draw 7 lines.

    for (int i = 0; i < 7; i++) {

        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth     = 0.5f;
        [path moveToPoint:CGPointMake(10, 10 + i * 10.3)];
        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];
        [path stroke];
    }
}

@end

其实就添加了下面的绘图代码而已,绘制7条线条,每条线条的宽度为0.5

drawRect中抗锯齿

效果如下

drawRect中抗锯齿

将图片放大后会发现,线条的宽度并不一致,有的颜色深,有的颜色浅,这就是开了抗锯齿之后的效果

drawRect中抗锯齿

修改代码关闭抗锯齿

//
//  DrawRectView.m
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.

//  Copyright © 2017年 TechCode. All rights reserved.

//

#import "DrawRectView.h"

@implementation DrawRectView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        self.backgroundColor   = [UIColor clearColor];
        self.layer.borderWidth = 0.5f;
        self.layer.borderColor = [UIColor redColor].CGColor;
    }

    return self;
}

- (void)drawRect:(CGRect)rect {

    // Get context.

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Sets anti-aliasing on.

    CGContextSetShouldAntialias(context, NO);

    // Set stroke color
    [[UIColor blackColor] setStroke];

    // Draw 7 lines.

    for (int i = 0; i < 7; i++) {

        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth     = 0.5f;
        [path moveToPoint:CGPointMake(10, 10 + i * 10.3)];
        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];
        [path stroke];
    }
}

@end

drawRect中抗锯齿

显示效果

drawRect中抗锯齿

图片放大后,线条宽度一致

drawRect中抗锯齿

结论

开了抗锯齿后,系统会对绘制的线条进行一定的模糊处理,来达到不容易看到狗牙的目的,什么是狗牙?你可以运行以下代码来看看两者之间的区别

//
//  DrawRectView.m
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.

//  Copyright © 2017年 TechCode. All rights reserved.

//

#import "DrawRectView.h"

@implementation DrawRectView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        self.backgroundColor   = [UIColor clearColor];
        self.layer.borderWidth = 0.5f;
        self.layer.borderColor = [UIColor redColor].CGColor;
    }

    return self;
}

- (void)drawRect:(CGRect)rect {

    // Get context.

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Sets anti-aliasing off.

    CGContextSetShouldAntialias(context, NO);

    // Set stroke color
    [[UIColor blackColor] setStroke];

    // Draw 7 lines.

    for (int i = 0; i < 7; i++) {

        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth     = 0.5f;
        [path moveToPoint:CGPointMake(0, 0 + i * 10.3)];
        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];
        [path stroke];
    }
}

@end

关闭抗锯齿后不会出现模糊现象,都会出现锯齿,俗称狗牙

drawRect中抗锯齿

drawRect中抗锯齿

打开抗锯齿功能之后线条会模糊,锯齿得到了一些缓解,称作抗锯齿

drawRect中抗锯齿

drawRect中抗锯齿

Original: https://www.cnblogs.com/YouXianMing/p/7451703.html
Author: YouXianMing
Title: drawRect中抗锯齿



相关阅读

Title: 统一代码风格工具——editorConfig

editorConfig不是什么软件,而是一个名称为 .editorconfig&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;。该文件用来定义项目的编码规范,编辑器的行为会与.editorconfig 文件中定义的一致,并且 &#x5176;&#x4F18;&#x5148;&#x7EA7;&#x6BD4;&#x7F16;&#x8F91;&#x5668;&#x81EA;&#x8EAB;&#x7684;&#x8BBE;&#x7F6E;&#x8981;&#x9AD8;,这在多人合作开发项目时十分有用而且必要

1. 编辑器是否支持editorConfig

2. 匹配和优先级

当打开一个文件时, editorConfig&#x63D2;&#x4EF6;会在打开文件的目录和其每一级父目录查找 .editorconfig&#x6587;&#x4EF6;,直到有一个配置文件 root=true

editorConfig的配置文件是从上往下读取的并且最近的editorConfig配置文件会被最先读取. 匹配EditorConfig配置文件中的配置项会按照读取顺序被应用, 所以 &#x6700;&#x8FD1;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x914D;&#x7F6E;&#x9879;&#x62E5;&#x6709;&#x4F18;&#x5148;&#x6743;

&#x5982;&#x679C;.editorconfig&#x6587;&#x4EF6;&#x6CA1;&#x6709;&#x8FDB;&#x884C;&#x67D0;&#x4E9B;&#x914D;&#x7F6E;&#xFF0C;&#x5219;&#x4F7F;&#x7528;&#x7F16;&#x8F91;&#x5668;&#x9ED8;&#x8BA4;&#x7684;&#x8BBE;&#x7F6E;

*                匹配除/之外的任意字符串
**               匹配任意字符串
?                匹配任意单个字符
[name]           匹配name中的任意一个单一字符
[!name]          匹配不存在name中的任意一个单一字符
 所有的属性和值都是忽略大小写的. 解析时它们都是小写的

1.root 表示是最顶层的配置文件,发现设为true时,才会停止查找.editorconfig文件

root = true

2.charset:文件编码。可选值

charset = latin1
          utf-8  常用
          utf-8-bom 不建议使用
          utf-16be
          utf-16le

3.indent_style: 缩进类型。可选值

indent_style = space 软缩进
                tab 硬缩进

4.indent_size: 缩进数量。可选值

indent_size = 整数, 一般设置 2 或 4。
              tab

5.tab_width: 一个制表位字符宽度

正整数, 当indent_size为数字的时候默认用indent_size

6.insert_final_newline:是否在文件的最后插入一个空行。可选值

insert_final_newline = true
                       false

7.end_of_line:换行符格式。说明见Wiki:换行。可选值

end_of_line = lf  常用
              crlf
              cr

8.trim_trailing_whitespace:是否删除行尾的空格。可选值

trim_trailing_whitespace = true
                           false
# http:

Original: https://www.cnblogs.com/exmyth/p/16225159.html
Author: 牧之丨
Title: 统一代码风格工具——editorConfig