美文网首页
图形上下文Graphics Context

图形上下文Graphics Context

作者: 得_道 | 来源:发表于2020-09-17 17:49 被阅读0次

    图形上下文(Graphics Context)是一个数据类型CGContextRef,它存放了Quartz 2D绘制的图形输出信息,可以看做是图形到设备输出的介质工具。Quartz 2D绘制的图形可以放到多种设备上,比如:PDF文件、显示器窗口、bitmap(位图)、view的Layer层等等,这就需要不同的Graphics Context来完成输出到不同设备上的工作。这好比人要上高速需要汽车,去海上需要船,去天空需要飞机,这里人相当于图形,高速路、海、天空相当于设备,而汽车、船、飞机就是Graphics Context。

    介绍如何为各种绘图目的地创建图形上下文。图形上下文在代码中由数据类型CGContextRef表示,它是一种不透明的数据类型。
    获得图形上下文后,可以使用Quartz 2D函数绘制上下文,对上下文执行操作(如旋转),以及更改图形状态参数(如线宽和填充颜色)

    Graphics Context的几种类型

    1. Bitmap Graphics Context(位图图形上下文):
      位图图形上下文用于将RGB图像,GMYK图像或者黑白图像绘制到一个位图(bitmap)对象中。

    2. PDF Graphics Context(PDF图形上下文):
      PDF图形上下文可以帮助开发者创建PDF文件,将内容绘制进PDF文件中,其与位图上下文最大的区别在于PDF数据可以保存多页图像。

    3. Window Graphics Context(窗口上下文):
      用于OS系统中的窗口绘制。

    4. Layer Context(图层上下文):
      用于将内容绘制在Layer图层上。

    5. Printer Graphics Context(打印上下文):
      使用Mac打印功能时,此上下文用于将内容绘制在打印输出源上。

    简单说明

    图形上下文(Graphics Context):是一个CGContextRef类型的数据

    图形上下文的作用:保存绘图信息、绘图状态

    决定绘制的输出目标(绘制到什么地方去?)(输出目标可以是PDF文件、Bitmap或者显示器的窗口上)


    image.png

    相同的一套绘图序列,指定不同的Graphics Context,就可将相同的图像绘制到不同的目标上。


    image.png

    1.创建Bitmap图形上下文的方法

    //方法1 UIGraphicsBeginImageContext(<#CGSize size#>);

    //方法2 UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)

    使用两个方法同样都可以创建,但是使用第一个方法将来创建的图片清晰度和质量没有第二种方法的好。

    //方法1
    //    UIGraphicsBeginImageContext(<#CGSize size#>);
    //方法2
    UIGraphicsBeginImageContextWithOptions( CGSizeMake(200, 200), NO, 0);
    //1.获取bitmap上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.绘图(画一个圆)
    CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 100, 100));
    //3.渲染
    CGContextStrokePath(ctx);
    //4.获取生成的图片
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    //5.显示生成的图片到imageview
    self.iv.image=image;
    //6.保存绘制好的图片到文件中
    //先将图片转换为二进制数据,然后再将图片写到文件中
    //    UIImageJPEGRepresentation(image, 1); //第二个参数为保存的图片的效果
    NSData *data=UIImagePNGRepresentation(image);
    [data writeToFile:@"/Users/apple/Desktop/abc.png" atomically:YES];
    

    2.创建PDF图形上下文的方法

    CGPDFContextCreateWithURL:当你需要用Core Foundation URL指定pdf输出的位置时使用该函数。

     CGContextRef myOutContext = NULL;
        CFURLRef url;
        url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, false);
     
        if (url != NULL) {
            myOutContext = CGPDFContextCreateWithURL (url,  inMediaBox,  NULL);
            CFRelease(url);
        }
    

    CGPDFContextCreate:当需要将pdf输出发送给数据用户时使用该方法。代码清单2-3显示了该函数的使用方法:

    CGContextRef MyPDFContextCreate (const CGRect *inMediaBox, CFStringRef path)
    {
        CGContextRef        myOutContext = NULL;
        CFURLRef            url;
        CGDataConsumerRef   dataConsumer;
     
        url = CFURLCreateWithFileSystemPath (NULL,  path, kCFURLPOSIXPathStyle, false);
     
        if (url != NULL)
        {
            dataConsumer = CGDataConsumerCreateWithURL (url);
            if (dataConsumer != NULL)
            {
                myOutContext = CGPDFContextCreate (dataConsumer, inMediaBox, NULL);
                CGDataConsumerRelease (dataConsumer);
            }
     
            CFRelease(url);
        }
     
        return myOutContext;
    }
    

    3. 创建Window图形上下文(Mac os中)

    在Mac OS X中绘制时,您需要创建一个适合您所使用的框架的窗口图形上下文。 Quartz 2D API本身不提供获取窗口图形上下文的函数。 相反,您使用Cocoa框架来获取在Cocoa中创建的窗口的上下文。

    您可以从Cocoa应用程序的drawRect:例程中使用以下代码行获取Quartz图形上下文:

    CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
    

    方法currentContext返回当前线程的NSGraphicsContext实例。 方法graphicsPort返回由接收器表示的低级,平台特定的图形上下文,这是一个Quartz图形上下文。

    - (void)drawRect:(NSRect)rect
    {
        CGContextRef myContext = [[NSGraphicsContext // 1
                                    currentContext] graphicsPort];
       // ********** Your drawing code here ********** // 2
        CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);// 3
        CGContextFillRect (myContext, CGRectMake (0, 0, 200, 100 ));// 4
        CGContextSetRGBFillColor (myContext, 0, 0, 1, .5);// 5
        CGContextFillRect (myContext, CGRectMake (0, 0, 100, 200));// 6
      }
    

    这里是代码所做的事情:

    1.获取视图的图形上下文。
    2.这是您插入绘图代码的位置。 以下四行代码是使用Quartz 2D函数的示例。
    3.设置完全不透明的红色填充颜色。 有关颜色和alpha(设置不透明度)的信息,请参阅颜色和颜色空间。
    4.填充原点为(0,0)且宽度为200,高度为100的矩形。有关绘制矩形的信息,请参阅路径。
    5.设置部分透明的蓝色填充颜色。
    6.填充原点为(0,0)且宽度为100,高度为200的矩形。

    4.创建Layer图形上下文

    //获取上下文 上下文的输出目标就是self.layer
    CGContextRef context = UIGraphicsGetCurrentContext();
    

    5.创建打印上下文

    Mac OS X中的Cocoa应用程序通过自定义的NSView子类来实现打印。一个视图通过调用print:方法来进行打印。然后视图以打印机为目标创建一个Graphics Context,并调用drawRect:方法。应用程序使用与在屏幕进行绘制相同的绘制代码。我们同样可以自定义drawRect: 方法将图形绘制到打印机。

    相关文章

      网友评论

          本文标题:图形上下文Graphics Context

          本文链接:https://www.haomeiwen.com/subject/oynlyktx.html