IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
// 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);
*/
}
IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等的更多相关文章
- ios Quartz 各种绘制图形用法
摘要: CoreGraphics的功能非常强大,可以绘制各种图形:今天学习一下怎么绘制简单的点线面,记录学习. 一.导入coreGraphics.framework 二.绘制图形 1.绘制矩形 // ...
- Quartz 2D(常用API函数、绘制图形、点线模式)
Quzrtz 2D 绘图的核心 API 是 CGContextRef ,它专门用于绘制各种图形. 绘制图形关键是两步: 1.获取 CGContextRef ; 2.调用 CGContextRef 的方 ...
- html5 canvas 笔记一(基本用法与绘制图形)
<canvas> 元素 <canvas id="tutorial" width="150" height="150"> ...
- iOS - Quartz 2D 画板绘制
1.绘制画板 1.1 绘制简单画板 PaintBoardView.h @interface PaintBoardView : UIView @end PaintBoardView.m @interfa ...
- IOS 绘制基本图形( 画圆、画线、画圆弧、绘制三角形、绘制四边形)
// 当自定义view第一次显示出来的时候就会调用drawRect方法- (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIG ...
- iOS - Quartz 2D 二维绘图
1.Quartz 2D 简介 Quartz 2D 属于 Core Graphics(所以大多数相关方法的都是以 CG 开头),是 iOS/Mac OSX 提供的在内核之上的强大的 2D 绘图引擎,并且 ...
- Quart 2D 绘制图形简单总结
0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGConte ...
- iPhone之Quartz 2D系列--图形上下文(2)(Graphics Contexts)
以下几遍关于Quartz 2D博文都是转载自:http://www.cocoachina.com/bbs/u.php?action=topic&uid=38018 iPhone之Quartz ...
- Quartz 2D - 图形上下文(Graphics Contexts)
一个Graphics Context表示一个绘制目标.它包含绘制系统用于完成绘制指令的绘制参数和设备相关信息.Graphics Context定义了基本的绘制属性,如颜色.裁减区域.线条宽度和样式信息 ...
随机推荐
- jqChart动态数据
<link rel="stylesheet" type="text/css" href="../../css/jquery.jqChart.cs ...
- 闪回还原点(Flashback Restore Point)
Flashback Restore Point(闪回还原点) 闪回还原点分两种,一种是Normal Restore Points(正常还原点),另一种是Guaranteed Restore Point ...
- 四种可变交流swap方法
1.void swap(int &x, int &y){ int temp=x; x=y; y=temp; } 2.void swap(int &x, int &y){ ...
- careercup-数组和字符串1.8
1.8 假定有一个方法isSubstring,可检查一个单词是否为其他字符串的子串.给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次isSubstring.旋转字符串: ...
- fork和exec函数
#include<unistd.h> pid_t fork(void); 返回:在子进程中为0,在父进程中为子进程IO,若出错则为- fork最困难之处在于调用它一次,它却返回两次.它在调 ...
- 最简单的自定义适配器adapter
下面是一个非常简单的自定义适配器的总体源码,从这个源码入门,就可以慢慢学会适配器了 适配器的作用: 完成数据和界面控件的绑定,把数据绑定到界面的现实控件条目上(对于listView,应该是listVi ...
- 将Java应用注册为后台服务
项目中有一个java应用程序,交付后用户要求要把这个程序做成后台服务程序,即:系统启动后该程序可以自动启动,并且在前台不要出现运行窗口,维护人员只要在“服务管理”(Windows)中选择启动或停止即可 ...
- JavaScript 开发规范要求详解
作为一名开发人员(We前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以及执行效率上的问题.本人在开发工作中就曾与不按规范来开发的同 ...
- Builder 生成器模式
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 当同时满足以下情况的时候可以使用Builder模式: 当创建复杂对象的算法应该独立于该对象的组成部分以及他们的装配方式. 当 ...
- 深入理解Javascript变量作用域
在学习JavaScript的变量作用域之前,我们应当明确几点: a.JavaScript的变量作用域是基于其特有的作用域链的. b.JavaScript没有块级作用域. c.函数中声明的变量在整个函数 ...