drawRect中抗锯齿
在开始之前,我们需要创建一个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中抗锯齿的更多相关文章
- CxImage图像库的使用 .
http://blog.csdn.net/wangjie0377/article/details/7830405 CxImage图像库 CxImage下载地址:http://www.codeproje ...
- 【转】CxImage图像库的使用
CxImage下载地址:http://www.codeproject.com/KB/graphics/cximage/cximage600_full.zip 作者:Davide Pizzolato C ...
- 【ShaderToy】基础篇之谈谈点、线的绘制
写在前面 写前面一篇的时候,发现还是不够基础.因此打算增加几篇基础篇,从点线面开始,希望可以更好理解. 其实用Pixel Shader的过程很像在纸上绘画的过程.屏幕上的每一个像素对应了纸上的一个方格 ...
- 【视频开发】 十全大补:CxImage图像处理类库
十全大补:CxImage图像处理类库 转载IT168 CxImage是一个可以用于MFC 的C++图像处理类库类,它可以打开,保存,显示,转换各种常见格式的图像文件,比如BMP, JP ...
- android.graphics(1) - Paint, Canvas, drawLine, drawPoint, drawRect, drawRoundRect, drawCircle, drawOval, drawArc
一.Paint与Canvas 像我们平时画图一样,需要两个工具,纸和笔.Paint就是相当于笔,而Canvas就是纸,这里叫画布. 所以,凡有跟要要画的东西的设置相关的,比如大小,粗细,画笔颜色,透明 ...
- Qt 2D绘图之二:抗锯齿渲染和坐标系统
一.抗锯齿渲染 1.1 逻辑绘图 图形基元的大小(宽度和高度)始终与其数学模型相对应,下图示意了忽略其渲染时使用的画笔的宽度的样子. 1.2 物理绘图(默认情况) 在默认的情况下,绘制会产生锯齿,并且 ...
- drawRect
1) 画笔设置 Paint.Style.STROKE 中空模式 paint = new Paint(); //新建一个画笔对象 paint.setAntiAlias(true);//抗锯齿功能 pai ...
- 用drawRect的方式实现一个尺子
用drawRect的方式实现了一个尺子选择器,demo在这里:https://github.com/Phelthas/LXMRulerView 效果如图: 如果不考虑复用的问题,我感觉最简单的实现 ...
- UIView的layoutSubviews和drawRect方法何时调用
首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubvi ...
随机推荐
- C#搭建Oauth2.0认证流程以及代码示例
我认为对于一个普遍问题,必有对应的一个简洁优美的解决方案.当然这也许只是我的一厢情愿,因为根据宇宙法则,所有事物总归趋于混沌,而OAuth协议就是混沌中的产物,不管是1...0a还是2.,单看版本号就 ...
- python全栈开发day60-django_app ORM 完整登录案例
day60 内容回顾: 1. HTTP协议: 1.请求(浏览器发给服务端的消息——request) 请求方法 URL 协议版本\r\n k1:v1\r\n k2:v2\r\n \r\n 请求体 —— ...
- 实现判断条件中有in的判断
如果是简单的写sql,在where中写死就可以了,但是如果是不确定的参数呢,这个时候就需要一点处理方式了. 1.后台的写法 String[] operateResult=new String[]{&q ...
- jenkins创建svn项目自动部署
Exec command ; cd /home/zhxjdk18/server/jenkins_project/owgd_site/admin; rm -rf * ; source .bashrc ; ...
- cookie的基本操作
设置,读取,删除 var odate=new Date(); odate.setDate(odate.getDate()+14); document.cookie='user=blue;expires ...
- 【基础】链表的储存结构说明(python)
[实现链表的添加] class aNode(): def __init__(self,data=None,nxt=None): self.data=data self.nxt=nxt class ru ...
- Anaconda 虚拟环境的使用
目录 前言 1. 创建虚拟环境 2. 虚拟环境管理 3. Conda虚拟环境的包管理 前言 今天把anaconda进行了滚动更新,实体环境python版本也相应从3.6跟新到了3.7.但是问题来了,之 ...
- muduo学习笔记(六) 多线程的TcpServer
目录 前言 多线程TcpServer EventLoopThreadPool 线程池设计模式 muduo中的使用 连接的建立.消息.销毁 on_connection on_message on_clo ...
- python urllib 库
urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...
- 用户体验—微软Edge浏览器
我现在使用的浏览器是win10自带的Edge浏览器 用户界面: 首先整体界面的话是清晰,一目了然,而且记住用户选择: 1.微软必应搜索的主题分类明确,查询简洁方便.查询语句简单: 2 .信息覆盖程度非 ...