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 ...
随机推荐
- ERROR 000732:Output Geodatabase:Dataset Database Connections\Connection to localhost.sde\SDE.Dataset does not exist or is not supported
ArcCatalog 10中向SDE 数据集导入要素类时,出错:ERROR 000732:Output Geodatabase:Dataset Database Connections\Connect ...
- Python的_文件操作
打开文件:file_obj=open("文件路径","模式",“编码”’) 其中模式有: r,以只读方式打开文件(默认) w,打开一个文件只用于写入(不存在则创 ...
- svn创建分支(branch/tag)出现“path”already exists
不用在visual svn中创建相应的目录,svn会自己创建目录,但是自己必须指定该目录名称. 比如:
- 有了这些,java IO就不愁了
IO的总结: java中相对路径和绝对路径的问题: 在web项目中,如果生成的文件前面没有 / 开头的话,表示的是生成的文件在当前项目的根目录下如student.txt在项目中刷新就能看到. 如果是以 ...
- sql的with as用法
http://www.cnblogs.com/linjiqin/archive/2013/06/24/3152667.html
- 爬虫之 案列1补充(pipelines优化)
1. 先打开settings.py文件将 'ITEM_PIPELINES'启动(取消注释即可) 2. spider代码 # -*- coding: utf-8 -*- import scrapy im ...
- img-html-2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 实现Java简单继承
面向对象练习-简单继承 一.完成教师类的创建 说明: id 代表身份证号 name 表示姓名 birth 表示出生日期 title 表示职称(讲师,副教授,教授等) 二.完成学生类的创建 说明: ma ...
- HDU 4635 Strongly connected (强连通分量+缩点)
<题目链接> 题目大意: 给你一张有向图,问在保证该图不能成为强连通图的条件下,最多能够添加几条有向边. 解题分析: 我们从反面思考,在该图是一张有向完全图的情况下,最少删去几条边能够使其 ...
- MAVEN 加载公共包 commons
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileu ...