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 ...
随机推荐
- JMeter实现登录初始化(类似LR的init函数功能实现)
1.项目背景 在做项目的性能测试过程中,发现系统的登录功能非常慢,所以,在涉及到登录才能操作的场景,尽量避开登录操作 解决方案: 首选设置“登录并生成签名值”线程组
- seafile+glusterfs 安装部署
今天在虚拟机上搭一下seafile,用于测试环境.此处安装的是社区免费版本的,可以使用一键自动安装(MySQL适用). 官方文档:https://manual-cn.seafile.com/ 1.一键 ...
- Python学习(三) —— 基本数据类型
基本数据类型 一.整数类型(int) 32位机器,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647 64位机器,整数的位数是64位,取值范围位 ...
- k8s教程
k8s教程地址 安装https://github.com/gjmzj/kubeaszhttps://github.com/opsnull/follow-me-install-kubernetes-cl ...
- ansible Invetory(管理主机信息)
1. 定义组机和组 inventory文件可以是许多格式之一,具体取决于您拥有的inventory插件. 对于这个例子, /etc/ansible/hosts的格式是一个INI(类似于Ansible的 ...
- HDU2732 Leapin' Lizards 网络流 最大流 SAP
原文链接http://www.cnblogs.com/zhouzhendong/p/8362002.html 题目传送门 - HDU2732 题意概括 给你一个网格,网格上的一些位置上有一只蜥蜴,所有 ...
- Redis数据库 02事务| 持久化| 主从复制| 集群
1. Redis事务 Redis不支持事务,此事务不是关系型数据库中的事务: Redis事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的 ...
- SSH框架学习------struts2前后台传值(二)
struts2的Action类一般都会继承(extends)ActionSupport类(Action类,即实现action的类)1.前传后:jsp里<input type="text ...
- mniui里面没有只显示年的控件,monthpicker显示年月,datepicker显示具体到天的日期
spinner无法出现下拉框,只能一下下的点击. combobox可以出现下拉框,但是一般情况是从url后台取值. 现在可以自己在js里定义需要的值. <td><input id=& ...
- HDU 5493 Queue 【线段树】
<题目链接> 题目大意:给你n个人的身高和他前面或者后面身高大于他的人的个数,求一个字典序最小的满足此条件的序列,如果不存在输出“impossible”. 解题分析: 因为要保证字典序最小 ...