zoukankan      html  css  js  c++  java
  • Quartz 2D在ios中的使用简述一:坐标体系

      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

  • 相关阅读:
    国内外常用学术论文搜索网站
    2019年全国高校sql数据库
    Flask无法访问(127.0.0.1:5000)的问题解决方法
    Windows终端命令行工具Cmder
    JQuery实现密码可见不可见
    区块链最大的难题及其解决方案
    python 下载 进度条
    centos 用docker 运行 cypress
    通过 centos Nginx 查看html (为后续 服务器生成html 方便访问)
    odoo 分享 PostgreSQL 语句2
  • 原文地址:https://www.cnblogs.com/bandy/p/4341538.html
Copyright © 2011-2022 走看看