IOS的画线方法 CGContext
来源:互联网 发布:用什么看网络电视免费 编辑:程序博客网 时间:2024/05/22 14:11
使用context画线制作的手表
源码下载地址:http://download.csdn.net/detail/q562679764/9367221
context画线
画直线
//获取view上下文所有画线方法的基础
CGContextRef context =UIGraphicsGetCurrentContext();
//改变线条颜色
CGContextSetStrokeColorWithColor(context, [UIColorgreenColor].CGColor);
//设置路径颜色
CGContextSetRGBStrokeColor(context,0.5,0.5,0.5,1);
//设置路径起始坐标
CGContextMoveToPoint(context,0,0);
//设置路径的终点坐标
CGContextAddLineToPoint(context,100,100);
//设置路径宽度
CGContextSetLineWidth(context,2.0);
//渲染路径
CGContextStrokePath(context);
//下面两句成对使用用于保存和释放上次绘图的状态 每次绘制最好是用这两句话包起来 这样设置的颜色和线条宽度不会变成通用设置了
//储存上下文的状态
CGContextSaveGState(context);
//释放保存的上下文
CGContextRestoreGState(context);
绘制文字
//创建一个字体对象并设置大小此方法无需引用上下文
UIFont *font = [UIFontboldSystemFontOfSize:24.0];
//创建一个字典设置字体大小和文字颜色
NSDictionary * dictionary = [[NSDictionaryalloc]initWithObjectsAndKeys:font,NSFontAttributeName,[UIColorredColor],NSForegroundColorAttributeName,nil];
//将文本内容渲染到view上
[@"123\n456"drawInRect:CGRectMake(20,40,320,480)withAttributes:dictionary];
绘制矩形
//设置路径颜色
CGContextSetRGBStrokeColor(context,0.5,0.5,0.5,1);
//绘制一个空心矩形的路径
CGContextAddRect(context,CGRectMake(0,0,50,50));
//设置无边框路径的填充颜色
CGContextSetRGBFillColor(context,1,1,0,1);
//绘制一个无边框的路径
CGContextFillRect(context,CGRectMake(50,50,50,50));
绘制椭圆
//设置一个空心圆形的路径
CGContextAddEllipseInRect(context,CGRectMake(100,100,50,50));
//设置一个实心圆
CGContextFillEllipseInRect(context,CGRectMake(150,150,60,50));
绘制贝兹曲线
//设置路径起始坐标
CGContextMoveToPoint(context,0,0);
//绘制3次曲线
CGContextAddCurveToPoint(context, 200, 100, 200, 200,200,100);
//绘制2次贝兹曲线
CGContextAddQuadCurveToPoint(context,150,10,300,220);
//绘制弧线,带角度
CGContextAddArcToPoint(context, 100,200, 300,200, 100);
绘制虚线
//设置虚线样式
CGFloat aa[] = {5,5,2,2};
//设置虚线 0表示从数组的第几个开始,4表示数组的长度 数组内表示绘点得样式为画5虚5画2虚2
CGContextSetLineDash(context, 0, aa, 4);
绘制渐变颜色
//绘制渐变颜色 这个方法还没搞明白 慎用
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);
绘制图片
1:无上下文绘制
//获取图片的地址返回一个NSString类型地址 无需上下文
NSString * imgPath = [[NSBundlemainBundle]pathForResource:@"地球"ofType:@"jpg"];
//获取地址中的文件
UIImage * myImg = [[UIImage alloc] initWithContentsOfFile:imgPath];
//直接使用下面的这个效果一样
UIImage * myImage = [UIImage imageNamed:@"地球.jpg"];
//将图片渲染到view上
[myImg drawInRect:CGRectMake(0,0,50,50)];
2:上下文绘制
//获取view的上下文 此方法绘出的图像是倒立的
CGContextRef context =UIGraphicsGetCurrentContext();
//获取图片
UIImage * img = [UIImage imageNamed:@"地球.jpg"];
//获取位图
CGImageRef image = img.CGImage;
//保存上下文的状态压栈
CGContextSaveGState(context);
//为视图设置大小
CGRect touchRect = CGRectMake(0,0, img.size.width, img.size.height);
//绘图
CGContextDrawImage(context, touchRect, image);
//出栈
CGContextRestoreGState(context);
3:旋转
//获取图片
UIImage * img = [UIImage imageNamed:@"地球.png"];
//获取位图
CGImageRef image = img.CGImage;
//储存上下文的状态
CGContextSaveGState(context);
//设置旋转中心
CGContextTranslateCTM(context,160,240);
//设置绘图的大小和坐标 XY以中心为准
CGRect touchRect = CGRectMake(10,10,50,50);
//旋转 这个方法写在设置中心和大小的前面和后面效果完全不同的
CGContextRotateCTM(context,angle * M_PI/180);
//绘制图片
CGContextDrawImage(context, touchRect, image);
//释放保存的上下文
CGContextRestoreGState(context);
- IOS的画线方法 CGContext
- CGContext画线
- CGContext 画线
- CGContext的方法使用
- iOS: CGContext的函数CGContextAddArc
- iOS: CGContext的函数CGContextAddArcToPoint
- iOS: CGContext的函数CGContextAddEllipseInRect
- iOS: CGContext的函数CGContextAddLines
- iOS: CGContext的函数CGContextAddLineToPoint
- iOS: CGContext的函数CGContextAddPath
- iOS: CGContext的函数CGContextAddQuadCurveToPoint
- iOS: CGContext的函数CGContextAddRect
- iOS: CGContext的函数CGContextSaveGState
- iOS: CGContext的函数CGContextBeginPage
- iOS: CGContext的函数CGContextBeginPath
- iOS: CGContext的函数CGContextBeginTransparencyLayer
- iOS: CGContext的函数CGContextBeginTransparencyLayerWithRect
- iOS: CGContext的函数CGContextClearRect
- DremWeaver编写的第一个企业化网站,希望大家点评下(附网站部分效果图)
- 关于程序猿的经典笑话
- leetCode #94 Binary Tree Inorder Traversal
- C++数组的排列组合
- myeclipse6.5配置tomcat7
- IOS的画线方法 CGContext
- cts-verify中BYOD Managed Provisioning ,涉及到low_memorykiller问题
- python对文件及文件夹的操作
- WEB请求过程的深入学习(深入分析JavaWeb技术内幕,第一章读后总结和深入学习)
- 秋天是掉头发的季节,怎么做才少掉些?!
- TCP 连接断连问题剖析
- MySQL中select * for update锁表的问题
- FOXMAIL无法响应
- CTS/GTS相关命令