Core Graphices 获取上下文的三种方式:

1、自定义view 重写view 的 drawRect:(CGRect)rect方法

- (void)drawRect:(CGRect)rect {

    /*
UIBezierPath *bezier = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 80, 80) byRoundingCorners:UIRectCornerTopLeft| UIRectCornerTopRight cornerRadii:CGSizeMake(40, 0)]; [[UIColor magentaColor ]setFill];
[[UIColor redColor] setStroke]; //Path operations on the current graphics context当前图形上下文中执行路径操作 [p fill];//只操作了填充 把路径在上下文中执行
[p stroke];//只操作了画线 */
/******************上面是ui方式 下面是core graphices *****************************/
//当前上下文及画布为当前view
CGContextRef con = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(con, CGRectMake(,,,)); CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
CGContextSetStrokeColorWithColor(con, [UIColor redColor].CGColor); // CGContextFillPath(con);//只操作了填充
// CGContextStrokePath(con);//只操作了画线
CGContextDrawPath(con, kCGPathFillStroke); }

draw的调用时机

1.如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。

2.该方法在调用sizeThatFits后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。

3.通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。

4.直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0.

注意:在UIView中绘制图形,获取的上下文就是这个view对应的layer的上下文。在渲染的时候,就是把图形渲染到对应的layer上。

  在执行渲染操作的时候,本质上它的内部相当于执行了 [self.layer drawInContext:ctx];

2、drawInContext:(CGContextRef)ctx

#import "GainContext_DrawInContext.h"

@implementation GainContext_DrawInContext

- (void)drawInContext:(CGContextRef)ctx{

//    UIGraphicsPushContext(ctx);
// UIBezierPath *bezier = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 20, 80, 80) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(40, 40)];
// [[UIColor magentaColor ]setFill];
// [[UIColor redColor] setStroke];
// [bezier fill];
// [bezier stroke];
// UIGraphicsPopContext(); /*************************************/ UIBezierPath *bezier = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(, , , ) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(, )]; CGContextAddPath(ctx, bezier.CGPath);
CGContextSetStrokeColorWithColor(ctx, [UIColor magentaColor].CGColor);
CGContextSetFillColorWithColor(ctx, [UIColor cyanColor].CGColor); // CGContextStrokePath(ctx);
// CGContextFillPath(ctx); CGContextDrawPath(ctx, kCGPathEOFillStroke); }
调用: 

 GainContext_DrawInContext *layer = [GainContext_DrawInContext layer];
layer.frame = CGRectMake(, , , );
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];

注意:在自定义layer中的-(void)drawInContext:方法不会自己调用,只能自己通过setNeedDisplay方法调用

3、设置CALayer的delegate,然后让delegate实现drawLayer:inContext:方法,当CALayer需要绘图时,会调用delegate的drawLayer:inContext:方法进行绘图。

#import <UIKit/UIKit.h>

@interface GainContext_Delegate : NSObject

@end

#import "GainContext_Delegate.h"

@implementation GainContext_Delegate

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
// UIGraphicsPushContext(ctx);
// CGPoint center = CGPointMake(40, 40);
// UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:40 startAngle:0 endAngle:50*M_PI/180 clockwise:NO];
// [path addArcWithCenter:center radius:20 startAngle:50*M_PI/180 endAngle:0 clockwise:YES];
// [[UIColor magentaColor] setFill];
// [path fill];
// UIGraphicsPopContext(); CGPoint center = CGPointMake(, );
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius: startAngle: endAngle:*M_PI/ clockwise:NO];
[path addArcWithCenter:center radius: startAngle:*M_PI/ endAngle: clockwise:YES];
[[UIColor magentaColor] setFill]; CGContextAddPath(ctx, path.CGPath);
CGContextSetStrokeColorWithColor(ctx, [UIColor magentaColor].CGColor);
CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
CGContextDrawPath(ctx, kCGPathFillStroke); } /***调用***/

self.delegate = [[GainContext_Delegate alloc] init];


CAShapeLayer *delegateShapLayer = [CAShapeLayer layer];


delegateShapLayer.frame  = CGRectMake(10, 20, 100, 100);


delegateShapLayer.delegate = self.delegate;


[delegateShapLayer setNeedsDisplay];


[self.view.layer addSublayer:delegateShapLayer];


 

 4、画图片

          UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];

         [self addSubview:imageView];

         UIGraphicsBeginImageContextWithOptions(CGSizeMake(, ), NO, [UIScreen mainScreen].scale);
CGContextRef contex = UIGraphicsGetCurrentContext(); UIBezierPath *bezier = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(, , , ) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(, )]; CGContextAddPath(contex, bezier.CGPath);
CGContextSetStrokeColorWithColor(contex, [UIColor magentaColor].CGColor);
CGContextSetFillColorWithColor(contex, [UIColor cyanColor].CGColor); CGContextDrawPath(contex, kCGPathFillStroke);
UIFont *font = [UIFont systemFontOfSize:];
NSString *string = @"Core Graphics";
[string drawAtPoint:CGPointMake(, ) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor redColor]}]; UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = im;

注意:coreGraphices 是不支持arc 的。。。

Core Graphices 获取上下文的更多相关文章

  1. Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor

    Webservice WCF WebApi   注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...

  2. 前后台获取上下文context

    1.web server端获取上下文:Context ctx = WafContext.getInstance().getContext();上下文中包含当前登录组织.当前登录用户.语种.数据库.客户 ...

  3. NET Core开发-获取所有注入(DI)服务

    NET Core开发-获取所有注入(DI)服务 获取ASP.NET Core中所有注入(DI)服务,在ASP.NET Core中加入了Dependency Injection依赖注入. 我们在Cont ...

  4. Core Graphices 设置渐变

    Core Graphices 设置渐变 Quartz 提供了两种设置渐变的方式  CGShadingRef and CGGradientRef 尝试CGGradientRef 的使用 import & ...

  5. 解决vue组件内前置路由守卫beforeRouteEnter无法获取上下文this

    问题描述 vue框架,只有在报名页面报名成功,然后自动跳转到订单详情,才弹出一个引流弹窗,其他情况均不弹出,我就想到使用vue 的组件内前置守卫beforeRouteEnter来实现.beforeRo ...

  6. .NET Core 反射获取所有控制器及方法上特定标签

    .NET Core 反射获取所有控制器及方法上特定标签 有个需求,就是在. NET Core中,我们想在项目 启动时,获取LinCmsAuthorizeAttribute这个特性标签所有出现的地方,把 ...

  7. Azure KeyVault(三)通过 Microsoft.Azure.KeyVault 类库在 .NET Core 上获取 Secrets

    一,引言 上一篇文章,我们介绍了 Azure Key Vault 在实际项目中的用途,Azure Key Vault 作为密钥管理的服务,我们可以很轻松的利用它创建和控制用于加密的密钥,和管理证书和机 ...

  8. .Net Core 获取上下文HttpContext

    1.先定义一个类 using Microsoft.AspNetCore.Http; namespace BCode.Util { public class MvcContext { public st ...

  9. EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下?

    前言 这个问题从未遇见过,是一位前辈问我EF Core内存泄漏问题时我才去深入探讨这个问题,刚开始我比较惊讶,居然还有这种问题,然后就有了本文,直接拿前辈的示例代码并稍加修改成就了此文,希望对在自学E ...

随机推荐

  1. vue的.vue文件是怎么run起来的(vue-loader)

    vue的.vue文件是怎么run起来的(vue-loader) 引子:vue的.vue文件是怎么跑起来的? 答:通过vue-loader,解析.vue文件,在webpack解析,拆解vue组件 1.v ...

  2. Charles设置HTTPS抓包

    1. 配置 Charles 根证书 点击 Help -> SSL Proxying -> Install Charles Root Certificate 之后会弹出钥匙串,如果不弹出,请 ...

  3. web在线智能四则运算挑战赛

    本网站主要针对小学生,是一个智能在线学习和测试平台,平台有精美炫酷的网页,和可靠的数据,主要特色,自动出题.验证码机制.非空检测.正则匹配不同年级同学而出不同难度的题目,在线结算.时间控制,时间一到自 ...

  4. leetcode python 011

    ####给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点.##绘制n条垂直线,使得线i的两个端点位于(i,ai)和(i,0).##找到两条线,它们与x轴一起形成一个容器,这 ...

  5. 如何避免form提交进行页面跳转

    正常的form表单提交后需要进行页面跳转,如果我们不希望进行页面跳转,那么按以下两个步骤,通过一个iframe就可以解决这个问题: 步骤一:首先在页面中定义一个空的不可见的iframe <!-- ...

  6. Spring 源码学习(1)—— 容器的基本实现

    最近在读Spring的源码,参考的是郝佳的<Spring源码深度解析>,这里把一些学习心得分享一下,总结的地方可能还有一些不完善,希望大家指教 IoC(控制反转)是Spring的特性之一, ...

  7. JavaWeb在线电子相册springmvc

    (一) 这学期上了一们叫做J2EE的选修课,本以为很水,没想到这个课这么烦气,最后大实验是一个springmvc的电子相册,做了两个周,终于完事了,姑且把这一篇叫做(一)吧 粗略看了一下啊,两个人的实 ...

  8. Fescar Quick Start

    Quick Start Let's begin with a Microservices example. Use case A business logic for user purchasing ...

  9. 新建虚拟机并与XShell连接(配置网卡)

    新建虚拟机并与XShell连接 一.新建虚拟机 下一步 下一步 选择CentOS版本,我的CentOS镜像是6.5版本64位,所以我选择CentOS 6 64. 然后点击下一步 可以自行更改位置,然后 ...

  10. win10 家庭版修改hosts的权限

    https://jingyan.baidu.com/article/624e7459b194f134e8ba5a8e.html