iOS CGContextRef/UIBezierPath(绘图)

绘图的底层实现方法

注意:在drawRect方法中系统会默认创建一个上下文(C语言类型)在其他方法中不会有这样一个上下文(可以自己测试)

@implementation DrawView
//注意,在drawRect方法中系统会默认创建一个上下文(C语言类型)
//在其他方法中不会有这样一个上下文(可以自己测试)
//下面这个方法中的rect参数会传入当前view的frame
- (void)drawRect:(CGRect)rect{
    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //或者绘图路径对象(可以了理解为画笔)
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    //绘图
    [bezierPath moveToPoint:CGPointMake(50, 50)];
    //通过控制点(ControlPoint来改变直线的弧度)
    [bezierPath addQuadCurveToPoint:CGPointMake(250, 250) controlPoint:CGPointMake(50, 250)];
    [bezierPath addLineToPoint:CGPointMake(250, 20)];
/**********设置上下文的状态***********/
    CGContextSetLineWidth(ctx, 10);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    //改变路径颜色
    [[UIColor redColor] setStroke];
/***********************************/
    //把路径添加到上下文
    CGContextAddPath(ctx, bezierPath.CGPath);
    //渲染上下文(layer)
    CGContextStrokePath(ctx);
}
@end

效果图:
绘图底层执行机制.png

绘制图片(简易)

- (void)drawRect:(CGRect)rect{
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 150, 150)];
    [[UIColor redColor] setStroke];
    [path setLineWidth:10];
    [[UIColor yellowColor] setFill];
    //fill和stroke必须在绘图的最后一步进行
    [path stroke];
    [path fill];
}

效果图:
绘制图片2.png

绘图三
- (void)drawRect:(CGRect)rect{
    //绘制扇形
    CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
    /*arcCenter:扇形圆心
     *radius:半径
     *clockWise:YES(逆时针),NO(顺时针)
     */
    //注意不能位self.center作为扇形的圆形:self.center是以screen的左上角位坐标原点进行绘图
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    [path addLineToPoint:center];
    [path closePath];
    [path setLineWidth:10];
    [[UIColor redColor] setStroke];
    [[UIColor yellowColor] setFill];
    [path stroke];
    [path fill];
    //fill方法中,有进行路径的自动关闭,使用了fill有时候可以不使用closePath
}

效果图:
绘图三.png

绘图三

注意
[self setNeedsDisplay];可以重制绘图内容,刷新UI

//ViewController Slider方法
- (IBAction)progressValue:(UISlider *)sender {
    NSString *progress = [NSString stringWithFormat:@"%.2f%%",sender.value *100];
    self.progressLabel.text = progress;
    self.drawView.progressValue = sender.value;
    [self.drawView drawRect:self.drawView.frame];
}
//drawView中的方法
- (void)setProgressValue:(CGFloat)progressValue{
    _progressValue = progressValue;
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect{
    CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
    CGFloat angle = self.progressValue*M_PI*2;
    CGFloat endAngle = angle - M_PI_2;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:rect.size.width*0.5 startAngle:-M_PI_2 endAngle:endAngle clockwise:YES];
    [path setLineWidth:10];
    [[UIColor redColor] setStroke];
    [path stroke];
}

效果图:
下载进度条.gif

绘图四 饼图

@implementation DrawView
- (void)drawRect:(CGRect)rect {
    //模拟获取数据
    NSArray *numberArr = @[@20,@30,@50,@70];
    CGFloat total = 0;
    CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.width*0.5);
    CGFloat radius = rect.size.width*0.5-10;
    for(NSNumber *number in numberArr){
        total += number.doubleValue;
    }
    CGFloat startAngel = 0;
    for(NSNumber *number in numberArr){
        CGFloat numberValue = number.doubleValue / total *M_PI *2;
        [self drawRadiusWith:center WithRadius:radius withStartAngle:startAngel wihtNumber:(CGFloat)numberValue];
        startAngel += numberValue;
    }
}
- (void)drawRadiusWith:(CGPoint)center WithRadius:(CGFloat)radius withStartAngle:(CGFloat)startAngle wihtNumber:(CGFloat)number{
    CGFloat endAngle = startAngle + number;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    [path setLineWidth:5];
     [path addLineToPoint:center];
    [[self randomColor] setFill];
    [path fill];
}
//设置随机颜色
- (UIColor *)randomColor{
    CGFloat red = arc4random_uniform(256) / 255.0;
    CGFloat blue = arc4random_uniform(256) / 255.0;
    CGFloat yellow = arc4random_uniform(256) / 255.0;
    return [UIColor colorWithRed:red green:blue blue:yellow alpha:1.0];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self setNeedsDisplay];
}
@end

效果图:
饼图(随机颜色).gif

绘文字

- (void)drawRect:(CGRect)rect {
    NSString *str = @"HELLO";
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    mutableDict[NSFontAttributeName] = [UIFont systemFontOfSize:50];
    mutableDict[NSForegroundColorAttributeName]  = [UIColor blueColor];
    mutableDict[NSStrokeColorAttributeName] = [UIColor redColor];
    mutableDict[NSStrokeWidthAttributeName]  = @5;
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset = CGSizeMake(30, 30);
    shadow.shadowColor = [UIColor yellowColor];
    mutableDict[NSShadowAttributeName] = shadow;
    //drawInRect 会匹配内容,绘图适配
    [str drawInRect:rect withAttributes:mutableDict];
}

效果图:
绘文字.png

绘图/UIImage/裁剪
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"folder"];
    //这是裁剪区域一定要在绘图前进行设置
    UIRectClip(CGRectMake(50, 50, 50, 50));
    [image drawAsPatternInRect:rect];
}

效果图:
裁剪:绘图UIImage.png

不再DrawRect中开启位图上下文

注意关闭上下文

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"folder"];
    //1.开启一个跟图片原始大小的上下文
    //第二个参数opaque:不透明度
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.把图片绘制到上下文中
    [image drawAtPoint:CGPointZero];
    //3.把文字绘制到上下文当中
    NSString *str = @"HELLO";
    [str drawAtPoint:CGPointMake(10, 20) withAttributes:nil];
    //4.把上下文中的所有内容绘制成一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.手动关闭上下文
    UIGraphicsEndImageContext();
    self.imageView.image = newImage;
}
@end

效果图类似于微博中的水印

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容