Core Graphices 获取上下文

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(0,0,100,100));
    
    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(10, 20, 80, 80) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(40, 40)];

    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(10, 20, 80, 80); [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(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];
    
    
    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、画图片

 1          UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
 2         
 3         [self addSubview:imageView];
 4         
 5         
 6         
 7         UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, [UIScreen mainScreen].scale);
 8         CGContextRef contex =  UIGraphicsGetCurrentContext();
 9         
10         UIBezierPath *bezier = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 80, 80) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(40, 40)];
11         
12         CGContextAddPath(contex, bezier.CGPath);
13         CGContextSetStrokeColorWithColor(contex, [UIColor magentaColor].CGColor);
14         CGContextSetFillColorWithColor(contex, [UIColor cyanColor].CGColor);
15         
16         
17         
18         CGContextDrawPath(contex, kCGPathFillStroke);
19         UIFont *font = [UIFont systemFontOfSize:15];
20         NSString *string = @"Core Graphics";
21         [string drawAtPoint:CGPointMake(0, 0) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor redColor]}];
22         
23         
24         UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
25         UIGraphicsEndImageContext();
26         imageView.image = im;

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

原文地址:https://www.cnblogs.com/xiaowuqing/p/7150620.html