CGContext又叫图形上下文,相当于一块画布,以堆栈形式存放,只有在当前 context上绘图才有效。iOS有分多种图形上下文,其中UIView自带提供的在drawRect:方法中通过 UIGraphicsGetCurrentContext获取,还有专门为图片处理的context,UIGraphicsBeginImageContext函数生成,还有pdf的context等等。

1.一共有3种使用context的场景,其中每种场景都有2种方法绘图

场景1:

//通过UIView的子类的drawRect:在上下文中绘制,该方法系统已准备好一个cgcontext,并放置在上下文栈顶,rect形参就是context的尺寸大小
//当一个UIView的backgroundColor为nil和opaque为YES时,产生的context的背景就为黑色的
- (void)drawRect:(CGRect)rect
{
    //1.使用UIKit在context上绘制,UIKit的所有操作只会在当前栈顶的context,所以需要注意当前栈顶的context是否你需要操作的上下文
    //UIImage,NSString,UIBezierPath,UIColor等可以直接在当前context上操作
    UIImage* image = [UIImage imageNamed:@"test.png"];
    NSLog(@"size:%@",NSStringFromCGSize(image.size));
    //UIImage直接在context上操作,指定在context的哪个坐标上绘制,大小是原图的尺寸,如果大小超出了context的范围就会被截取掉
   // [image drawAtPoint:CGPointMake(100, 100)];
    //指定在context的哪个坐标上绘制,并指定绘制的图片尺寸大小,这样图片的尺寸就会被压缩,不会超出context范围
    [image drawInRect:CGRectMake(0, 0, rect.size.width/2, rect.size.height/2)];

//2.使用Core Graphics的函数在context上绘制,Core Graphics的函数需要context作为参数,只绘制在指定使用的context上
    //功过UIGraphicsGetCurrentContext函数获取当前上下文栈顶的context,UIView系统已为其准备好context并存放在栈顶了
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    //画一个椭圆
//    CGContextAddEllipseInRect(context, CGRectMake(0,0,100,100));
//    //填充颜色为蓝色
//    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
//    //在context上绘制
//    CGContextFillPath(context);

场景2:

//实现该方法,用于CALayer回调,CALayer通过它的代理类来进行绘图操作,切记千万不能把UIView作为CALayer的代理类,因为UIView自身有隐式的图层,若再把显式的图层赋给它会发生不知名错误的
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx
{
    //1.使用UIKit进行绘制,因为UIKit只会对当前上下文栈顶的context操作,所以要把形参中的context设置为当前上下文
    UIGraphicsPushContext(ctx);
    UIImage* image = [UIImage imageNamed:@"test.png"];
    //指定位置和大小绘制图片
    [image drawInRect:CGRectMake(0, 0,100 , 100)];
    UIGraphicsPopContext();
    
    //    UIGraphicsPushContext(ctx);
    //2.使用Core Graphics进行绘制,需要显式使用context
    //    //画一个椭圆
    //    CGContextAddEllipseInRect(ctx, CGRectMake(0,0,100,100));
    //    //填充颜色为蓝色
    //    CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
    //    //在context上绘制
    //    CGContextFillPath(ctx);
    //    UIGraphicsPopContext();
}

LayerDelegate* delegate = [[LayerDelegate alloc]init];
    CALayer* layer = [CALayer layer];
    layer.anchorPoint = CGPointMake(0, 0);
    layer.position = CGPointMake(100, 100);
    layer.bounds = CGRectMake(0, 0, 200, 200);
    layer.delegate = delegate;
    //需要显式调用setNeedsDisplay来刷新才会绘制layer
    [layer setNeedsDisplay];
    [self.view.layer addSublayer:layer];

下图为场景1和场景2的显示效果:

场景3:

//通过自己创建一个context来绘制,通常用于对图片的处理
    /*
     解释一下UIGraphicsBeginImageContextWithOptions函数参数的含义:第一个参数表示所要创建的图片的尺寸;第二个参 数用来指定所生成图片的背景是否为不透明,如上我们使用YES而不是NO,则我们得到的图片背景将会是黑色,显然这不是我想要的;第三个参数指定生成图片 的缩放因子,这个缩放因子与UIImage的scale属性所指的含义是一致的。传入0则表示让图片的缩放因子根据屏幕的分辨率而变化,所以我们得到的图 片不管是在单分辨率还是视网膜屏上看起来都会很好。
     */
    //该函数会自动创建一个context,并把它push到上下文栈顶,坐标系也经处理和UIKit的坐标系相同
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(context, CGRectMake(0,0,100,100));
    //填充颜色为蓝色
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    //在context上绘制
    CGContextFillPath(context);
    //把当前context的内容输出成一个UIImage图片
    UIImage* i = UIGraphicsGetImageFromCurrentImageContext();
    //上下文栈pop出创建的context
    UIGraphicsEndImageContext();
    [i drawInRect:CGRectMake(0, 0, 100, 100)];

下图为绘制的显示效果:

2.把整个屏幕转化为图片

UIImageView* imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    UIGraphicsBeginImageContextWithOptions(imageV.frame.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //把当前的整个画面导入到context中,然后通过context输出UIImage,这样就可以把整个屏幕转化为图片
    [self.view.layer renderInContext:context];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    imageV.image = image;
    UIGraphicsEndImageContext();

3.剪裁图片

//对一张图片进行剪裁
    CGImageRef imageref = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(100, 100, 200, 50));
    UIImageView* cropImage = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 200, 50)];
    cropImage.image = [UIImage imageWithCGImage:imageref];

CGImageRelease(imageref);

[self.view addSubview:cropImage];

下图下放的是剪裁后的图片效果:

CGContext的更多相关文章

  1. CGContext 的一些工具方法

    /* *设置虚线 *param context CGContext *param context CGContext *param lineDashLengths 如: const CGFloat l ...

  2. (转)IOS开发之——绘图(CGContext)

    周刊 更多 登录   IOS开发之——绘图(CGContext) 时间 2014-04-21 09:17:43 CSDN博客 原文  http://blog.csdn.net/zhenyu521131 ...

  3. CoreGraphics --- CGContext

    CGContext又叫图形上下文,相当于一块画布,以堆栈形式存放,只有在当前context上绘图才有效.iOS有分多种图形上下文,其中UIView自带提供的在drawRect:方法中通过UIGraph ...

  4. IOS开发之——绘图(CGContext)

    0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGContex ...

  5. iOS之CGcontext.h方法和属性简介

    /* CoreGraphics - CGContext.h Copyright (c) 2000-2012 Apple Inc. All rights reserved. */ #ifndef CGC ...

  6. 自定义圆饼(利用贝塞尔曲线和CGContext类完成)

    -(void)drawRect:(CGRect)rect{ CGFloat w = self.bounds.size.width; CGFloat h = self.bounds.size.heigh ...

  7. CGContext 解释

    Managing Graphics Contexts:管理图形上下文 CGContextFlush // 强制立即渲染未执行在一个窗口上下文上的绘图操作到目标设备.系统会在合适的时机自动调用此函数,手 ...

  8. CGContext绘图

    0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGContex ...

  9. CoreGraphics之CGContext绘图

    0  CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 
1 CGContextMoveToPoint 开始画线
 2 CGCon ...

随机推荐

  1. 通过maven创建自己的archetype

    最近项目组做好一套框架,为了推广需要创建一些空白项目给项目组使用,因为所有的空白项目里面的配置基本上都是一样的,为了减少重复工作,想通过maven创建一个自己的archetype,于是在网上大致搜了一 ...

  2. 找不到类型“IBatisService.boxManageService”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。

    找不到类型“IBatisService.boxManageService”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/s ...

  3. Python(四):数字连珠2

    对上次的代码作了一些修改.在码的过程中发现,最核心的部分是在横向.竖向和两个对角方向上找到5个以上相同的数字. 自己的思路是将x行y列所在的x行.y列,以及以此为交叉点的两点对角线上的数字,转化成字符 ...

  4. python文件_写入

    1.输入的数据写入到一个文本: #这个写入操作不会将原来的数据覆盖掉 n=raw_input('请输入写入的文件数据:') fl2=open('g:/2.txt','a') fl2.write('\n ...

  5. Android平台上使用气压传感器计算海拔高度

    气压传感器两年前已经开始被手机制造商运用在其设备上,但貌似没有引起开发者足够的重视.像Galaxy S III .Galaxy Note 2和小米2手机上都有,不过大家对于气压传感器比较陌生.其实大气 ...

  6. java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils

    java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils Caused by: java.lang.ClassNotFou ...

  7. 开心菜鸟系列学习笔记------------javascript(6)

    一.作用域链            1)函数的生命周期:            函数的生命周期分为创建和激活阶段(调用时),让我们详细研究它.            作用域链与一个执行上下文相关,变量 ...

  8. Effective Java2读书笔记-类和接口(二)

    第15条:使可变性最小化 通过一个复数类来看不可变类. public final class Complex { private final double re; private final doub ...

  9. eclipse安装github插件egit

    http://jingyan.baidu.com/article/4853e1e529483c1909f726c3.html help->InstallNew Software,这是安装插件推荐 ...

  10. .NET自动字符编码识别程序库 NChardet

    什么是NChardet NChardet是mozilla自动字符编码识别程序库chardet的.NET实现,它移植自jchardet,chardet的java版实现,可实现对给定字符流的编码探测. N ...