JavaShuo
栏目
标签
IOS Quartz 各类绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
时间 2019-11-08
标签
ios
quartz
各类
绘制
图形
用法
实现
画图
写文
画线
椭圆
矩形
栏目
iOS
繁體版
原文
原文链接
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (
void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
/*NO.1画一条线
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 200,20);
CGContextStrokePath(context);
*/
/*NO.2写文字
CGContextSetLineWidth(context, 1.0);
CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5);
UIFont *font = [UIFont boldSystemFontOfSize:18.0];
[@"公司:北京中软科技股份有限公司\n部门:ERP事业部\n姓名:McLiang" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font];
*/
/*NO.3画一个正方形图形 没有边框
CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);
CGContextFillRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);
*/
/*NO.4画正方形边框
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);
*/
/*NO.5画方形背景颜色
CGContextTranslateCTM(context, 0.0f, self.bounds.size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
UIGraphicsPushContext(context);
CGContextSetLineWidth(context,320);
CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0);
CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460));
UIGraphicsPopContext();
*/
/*NO.6椭圆
CGRect aRect= CGRectMake(80, 80, 160, 100);
CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextAddEllipseInRect(context, aRect); //椭圆
CGContextDrawPath(context, kCGPathStroke);
*/
/*NO.7
CGContextBeginPath(context);
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
CGContextMoveToPoint(context, 100, 100);
CGContextAddArcToPoint(context, 50, 100, 50, 150, 50);
CGContextStrokePath(context);
*/
/*NO.8渐变
CGContextClip(context);
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{
204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00,
29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00,
0.0 / 255.0, 50.0 / 255.0, 126.0 / 255.0, 1.00,
};
CGGradientRef gradient = CGGradientCreateWithColorComponents
(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
CGColorSpaceRelease(rgb);
CGContextDrawLinearGradient(context, gradient,CGPointMake
(0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),
kCGGradientDrawsBeforeStartLocation);
*/
/* NO.9四条线画一个正方形
//画线
UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0];
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextSetLineWidth(context, 4.0);
CGPoint aPoints[5];
aPoints[0] =CGPointMake(60, 60);
aPoints[1] =CGPointMake(260, 60);
aPoints[2] =CGPointMake(260, 300);
aPoints[3] =CGPointMake(60, 300);
aPoints[4] =CGPointMake(60, 60);
CGContextAddLines(context, aPoints, 5);
CGContextDrawPath(context, kCGPathStroke); //开始画线
*/
/* NO.10
UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0];
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);
CGContextSetFillColorWithColor(context, aColor.CGColor);
//椭圆
CGRect aRect= CGRectMake(80, 80, 160, 100);
CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextAddRect(context, rect); //矩形
CGContextAddEllipseInRect(context, aRect); //椭圆
CGContextDrawPath(context, kCGPathStroke);
*/
/* NO.11
画一个实心的圆
CGContextFillEllipseInRect(context, CGRectMake(95, 95, 100.0, 100));
*/
/*NO.12
画一个菱形
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);
*/
/*NO.13 画矩形
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
*/
/*椭圆
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
*/
/*用红色填充了一段路径:
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(context);
*/
/*填充一个蓝色边的红色矩形
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, rectangle);
*/
/*画弧
//弧线的是经过指定两个切点,还有角度,调用CGContextAddArcToPoint()绘制
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 100, 100);
CGContextAddArcToPoint(context, 100,200, 300,200, 100);
CGContextStrokePath(context);
*/
/*
绘制贝兹曲线
//贝兹曲线是经过移动一个起始点,而后经过两个控制点,还有一个停止点,调用CGContextAddCurveToPoint() 函数绘制
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 10, 10);
CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);
CGContextStrokePath(context);
*/
/*绘制二次贝兹曲线
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 10, 200);
CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);
CGContextStrokePath(context);
*/
/*绘制虚线
CGContextSetLineWidth(context, 5.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGFloat dashArray[] = {2,6,4,2};
CGContextSetLineDash(context, 3, dashArray, 4);//跳过3个再画虚线,因此刚开始有6-(3-2)=5个虚点
CGContextMoveToPoint(context, 10, 200);
CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);
CGContextStrokePath(context);
*/
/*绘制图片
NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];
UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath];
//[myImageObj drawAtPoint:CGPointMake(0, 0)];
[myImageObj drawInRect:CGRectMake(0, 0, 320, 480)];
NSString *s = @"个人小狗";
[s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]];
*/
/*
NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
CGImageRef image = img.CGImage;
CGContextSaveGState(context);
CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, touchRect, image);
CGContextRestoreGState(context);
*/
/*NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
CGImageRef image = img.CGImage;
CGContextSaveGState(context);
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -img.size.width, -img.size.height);
CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, touchRect, image);
CGContextRestoreGState(context);*/
/*
NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
CGImageRef image = img.CGImage;
CGContextSaveGState(context);
CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI);
myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height);
CGContextConcatCTM(context, myAffine);
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -img.size.width, -img.size.height);
CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, touchRect, image);
CGContextRestoreGState(context);
*/
}
相关文章
1.
iOS Quartz 各类绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
2.
用CGContextRef画各类图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形
3.
python PIL画线 矩形 椭圆 画图
4.
C# 图形的绘制矩形椭圆
5.
iOS 画图(矩形、圆、椭圆、弧度、扇形。。。)
6.
Graphics 框架画各类图形(圆、三角形、扇形、弧线、圆角矩形等等)
7.
Java绘图: 使用 Graphics 类绘制线段、矩形、椭圆/圆弧/扇形、图片、文本
8.
OpenCV画线、矩形、圆形
9.
MFC画图板的点、线、曲线、矩形、椭圆、多边形的实现
10.
OpenCV画椭圆、实心圆、凹多边形、线段、矩形
更多相关文章...
•
R 绘图 - 条形图
-
R 语言教程
•
PHP imagearc - 画椭圆弧
-
PHP参考手册
•
使用Rxjava计算圆周率
•
☆基于Java Instrument的Agent实现
相关标签/搜索
画影图形
图形
形图
椭圆形
矩形
圆形
字形
线形
画图
Quartz
iOS
红包项目实战
PHP教程
SQLite教程
应用
算法
学习路线
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
以实例说明微服务拆分(以SpringCloud+Gradle)
2.
idea中通过Maven已经将依赖导入,在本地仓库和external libraries中均有,运行的时候报没有包的错误。
3.
Maven把jar包打到指定目录下
4.
【SpringMvc】JSP+MyBatis 用户登陆后更改导航栏信息
5.
在Maven本地仓库安装架包
6.
搭建springBoot+gradle+mysql框架
7.
PHP关于文件$_FILES一些问题、校验和限制
8.
php 5.6连接mongodb扩展
9.
Vue使用命令行创建项目
10.
eclipse修改启动图片
本站公众号
欢迎关注本站公众号,获取更多信息
相关文章
1.
iOS Quartz 各类绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
2.
用CGContextRef画各类图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形
3.
python PIL画线 矩形 椭圆 画图
4.
C# 图形的绘制矩形椭圆
5.
iOS 画图(矩形、圆、椭圆、弧度、扇形。。。)
6.
Graphics 框架画各类图形(圆、三角形、扇形、弧线、圆角矩形等等)
7.
Java绘图: 使用 Graphics 类绘制线段、矩形、椭圆/圆弧/扇形、图片、文本
8.
OpenCV画线、矩形、圆形
9.
MFC画图板的点、线、曲线、矩形、椭圆、多边形的实现
10.
OpenCV画椭圆、实心圆、凹多边形、线段、矩形
>>更多相关文章<<