Quartz 2D在ios中的使用简述一:坐标体系
来源:互联网 发布:数据可视化公司招聘 编辑:程序博客网 时间:2024/06/05 18:34
转载自:http://www.cnblogs.com/bandy/p/4341538.html
Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境,官方文档: Quartz 2D Programming Guide 。
一、坐标体系
这样的坐标体系就导致我们使用Quartz 2D画的图是倒转的,我们要做以下处理才能得到我们想要的图片效果:
1.画布延Y轴下移height
2.对Y轴做垂直翻转
这2个步骤的代码为:
1 CGContextTranslateCTM(context, 0, height);2 CGContextScaleCTM(context, 1.0, -1.0);
测试代码为:
//代码1- (void)drawRect:(CGRect)rect { CGContextRef context=UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(context, 1, 0, 0, 1); CGContextFillRect(context, CGRectMake(0, 100, 100, 100)); NSString *text=@"文字"; UIFont *font=[UIFont systemFontOfSize:14]; [text drawAtPoint:CGPointMake(0, 200) withAttributes:font.fontDescriptor.fontAttributes]; UIImage *img=[UIImage imageNamed:@"gg.jpg"]; [img drawInRect:CGRectMake(0, 300, 100, 100)];}
//代码2-(id)initWithFrame:(CGRect)frame{ if (self=[super initWithFrame:frame]) { [self setBackgroundColor:[UIColor redColor]]; UIImageView *imgview=[[UIImageView alloc] initWithFrame:self.bounds]; CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB(); float width=self.bounds.size.width; float height=self.bounds.size.height; //256=10000000 int bitsPerComponent=8; //RGBA*8*width int bytesPerRow=4*8*width; CGContextRef context=CGBitmapContextCreate(NULL, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault); //翻转画布 CGContextTranslateCTM(context, 0, height); CGContextScaleCTM(context, 1.0, -1.0); UIGraphicsPushContext(context); //画布透明 //CGContextFillRect(context, self.bounds); CGContextSetRGBFillColor(context, 1, 0, 0, 1); CGContextSetRGBStrokeColor(context, 0, 1, 0, 1); CGContextFillRect(context, CGRectMake(0, 100, 100, 100)); NSString *text=@"文字"; UIFont *font=[UIFont systemFontOfSize:14]; [text drawAtPoint:CGPointMake(0, 200) withAttributes:font.fontDescriptor.fontAttributes]; UIImage *img=[UIImage imageNamed:@"gg.jpg"]; [img drawInRect:CGRectMake(0, 300, 100, 100)]; CGImageRef cgimg = CGBitmapContextCreateImage(context); UIImage *resultImg = [UIImage imageWithCGImage:cgimg]; CGContextRelease(context); CGColorSpaceRelease(colorSpace); CGImageRelease(cgimg); imgview.image=resultImg; [self addSubview:imgview]; } return self;}
2段代码实现的效果一样,代码1并没有做翻转操作,那是因为UIKit在UIGraphicsGetCurrentContext()得到的画布已经帮我们适应好了UIKit的坐标体系。
转载请注明出处:http://www.cnblogs.com/bandy/p/4341538.html
0 0
- Quartz 2D在ios中的使用简述一:坐标体系
- iOS开发 : 坐标变换Quartz 2D中的CGContextTranslateCTM、CGContextScaleCTM
- Quartz 2D中的坐标变换
- iOS中的Quartz 2D
- iOS--坐标变换Quartz 2D中的CGContextTranslateCTM、CGContextScaleCTM、CGContextRotateCTM、CGContextConcatCTM等
- iOS--坐标变换Quartz 2D中的CGContextTranslateCTM、CGContextScaleCTM、CGContextRotateCTM、CGContextConcatCTM等
- iOS--坐标变换Quartz 2D中的CGContextTranslateCTM、CGContextScaleCTM、
- 坐标变换Quartz 2D
- iOS 使用Quartz 2D画虚线
- iOS 使用Quartz 2D画虚线
- iOS 使用Quartz 2D画虚线
- iOS 使用Quartz 2D画虚线
- iOS 使用Quartz 2D画线
- iOS 使用Quartz 2D画虚线
- iOS 使用Quartz 2D画虚线
- iOS 使用Quartz 2D画虚线
- iOS 使用Quartz 2D画虚线
- iOS 使用Quartz 2D画虚线
- onclick vs addEventListener
- 理解hasOwnProperty()的作用
- add event listener on elements created dynamically
- ubuntu16.04+win7双系统的问题及解决(慢慢更新)
- 【iOS开发】Quartz2D的简单使用
- Quartz 2D在ios中的使用简述一:坐标体系
- Quartz 2D绘画直线,四边形,三角形,圆,圆弧的基本方法【原创】
- 学习并运用JavaScript的原生函数(bind讲得清楚)
- 欢迎使用CSDN-markdown编辑器
- confluence 构建企业wiki
- mysql 拒绝远程主机连接问题
- tar命令的详细解释
- QuartZ2D __ 简单用法 1
- [Swift]使用Quartz 2D进行绘图