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

其初始代码为

//
// DrawRectView.h
// CGContextSetShouldAntialias
//
// Created by YouXianMing on 2017/8/30.
// Copyright © 2017年 TechCode. All rights reserved.
// #import <UIKit/UIKit.h> @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并居中)

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

修改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 = ; i < ; i++) { UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 0.5f;
[path moveToPoint:CGPointMake(, + i * 10.3)];
[path addLineToPoint:CGPointMake( + , + i * 10.3)];
[path stroke];
}
} @end

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

效果如下

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

修改代码关闭抗锯齿

//
// 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 = ; i < ; i++) { UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 0.5f;
[path moveToPoint:CGPointMake(, + i * 10.3)];
[path addLineToPoint:CGPointMake( + , + i * 10.3)];
[path stroke];
}
} @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;
} - (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 = ; i < ; i++) { UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 0.5f;
[path moveToPoint:CGPointMake(, + i * 10.3)];
[path addLineToPoint:CGPointMake( + , + i * 10.3)];
[path stroke];
}
} @end

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

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

drawRect中抗锯齿的更多相关文章

  1. CxImage图像库的使用 .

    http://blog.csdn.net/wangjie0377/article/details/7830405 CxImage图像库 CxImage下载地址:http://www.codeproje ...

  2. 【转】CxImage图像库的使用

    CxImage下载地址:http://www.codeproject.com/KB/graphics/cximage/cximage600_full.zip 作者:Davide Pizzolato C ...

  3. 【ShaderToy】基础篇之谈谈点、线的绘制

    写在前面 写前面一篇的时候,发现还是不够基础.因此打算增加几篇基础篇,从点线面开始,希望可以更好理解. 其实用Pixel Shader的过程很像在纸上绘画的过程.屏幕上的每一个像素对应了纸上的一个方格 ...

  4. 【视频开发】 十全大补:CxImage图像处理类库

     十全大补:CxImage图像处理类库 转载IT168        CxImage是一个可以用于MFC 的C++图像处理类库类,它可以打开,保存,显示,转换各种常见格式的图像文件,比如BMP, JP ...

  5. android.graphics(1) - Paint, Canvas, drawLine, drawPoint, drawRect, drawRoundRect, drawCircle, drawOval, drawArc

    一.Paint与Canvas 像我们平时画图一样,需要两个工具,纸和笔.Paint就是相当于笔,而Canvas就是纸,这里叫画布. 所以,凡有跟要要画的东西的设置相关的,比如大小,粗细,画笔颜色,透明 ...

  6. Qt 2D绘图之二:抗锯齿渲染和坐标系统

    一.抗锯齿渲染 1.1 逻辑绘图 图形基元的大小(宽度和高度)始终与其数学模型相对应,下图示意了忽略其渲染时使用的画笔的宽度的样子. 1.2 物理绘图(默认情况) 在默认的情况下,绘制会产生锯齿,并且 ...

  7. drawRect

    1) 画笔设置 Paint.Style.STROKE 中空模式 paint = new Paint(); //新建一个画笔对象 paint.setAntiAlias(true);//抗锯齿功能 pai ...

  8. 用drawRect的方式实现一个尺子

    用drawRect的方式实现了一个尺子选择器,demo在这里:https://github.com/Phelthas/LXMRulerView 效果如图:   如果不考虑复用的问题,我感觉最简单的实现 ...

  9. UIView的layoutSubviews和drawRect方法何时调用

    首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubvi ...

随机推荐

  1. Linux批量清空当前目录中的日志文件

    在Linux中,有时需要批量清空当前目录中的日志文件,同时还要保留日志文件. 其实一行shell命令就可以搞定,一起来看看吧. 在当前目录下,键入如下命令: for i in `find . -nam ...

  2. k8s教程

    k8s教程地址 安装https://github.com/gjmzj/kubeaszhttps://github.com/opsnull/follow-me-install-kubernetes-cl ...

  3. Centos7服务器中通过编译源码安装MySQL

    基于在Centos7服务器上使用 yum 安装MySQL5.7到默认路径 在修改文件存储位置的时候,折腾了一番没有将成功将datadir修改为我想要的位置 我决定再尝试一下通过编译源码来自定义安装: ...

  4. 利用图片的灰度平均值来进行分类实现手写图片识别(数据集50000张图片)——Jason niu

    from collections import defaultdict import mnist_loader def main(): training_data, validation_data, ...

  5. POJ 1284 Primitive Roots (欧拉函数+原根)

    <题目链接> 题目大意: 满足{ ( $x^{i}$ mod p) | 1 <=$i$ <= p-1 } == { 1, …, p-1 }的x称为模p的原根.给出p,求原根个数 ...

  6. HDU 2841-Visible Trees 【容斥】

    <题目链接> 题目大意: 有一个农民,站在(0,0)点,从(1,1)点到(m,n)点每个点上有棵树,问这个农民能看到多少棵树.(如果多棵树在同一条直线上,那么他只能看到一颗) 解题分析: ...

  7. springboot2.0 redis EnableCaching的配置和使用

    一.前言 关于EnableCaching最简单使用,个人感觉只需提供一个CacheManager的一个实例就好了.springboot为我们提供了cache相关的自动配置.引入cache模块,如下. ...

  8. pyspider 启动错误

    [root@localhost python]# pyspider all [W 180629 07:08:26 run:413] phantomjs not found, continue runn ...

  9. SpringBoot统一处理异常

    在springboot项目,报错有着默认的提示,这篇文章介绍一下如何统一处理异常. 新建项目,pom文件如下: <?xml version="1.0" encoding=&q ...

  10. C# 的Chart

    Axis Label 横纵坐标的文字 (比如 0 20 40 ....) Axis Title 横纵坐标的代表什么(比如 Y Axis Title) Chart Area 图标所在位置 Chart P ...