美文网首页iOS渲染和动画
CGContextRef日常使用

CGContextRef日常使用

作者: 大哈哈哈哈 | 来源:发表于2018-08-07 10:54 被阅读13次

日常更新CGContextRef的使用:

1、获取某个View的截图

 UIGraphicsBeginImageContext(view.frame.size);
 CGContextRef context = UIGraphicsGetCurrentContext();
 [view.layer renderInContext:context];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext(); 

2、画一个文本

- (void)drawText:(NSString *)text
{
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextSetLineWidth(context, 1.0);//线条宽度
   CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5);//颜色
   UIFont *font = [UIFont boldSystemFontOfSize:18.0];
   [text drawInRect:CGRectMake(20, 40, 280, 300) withAttributes:@{NSFontAttributeName:font}];
}

3、 画一条线

- (void)drawLine
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);
    CGContextMoveToPoint(context, 140, 30);
    CGContextAddLineToPoint(context, 100, 100);
    CGContextStrokePath(context);
}

4、 画矩形

- (void)drawRectangle
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);
    CGContextStrokeRect(context, CGRectMake(100, 100, 100, 100));
    CGContextStrokePath(context);
}

5、 画椭圆

- (void)drawEllipse
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect aRect= CGRectMake(80, 180, 160, 100);
    CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);
    CGContextSetLineWidth(context, 3.0);
    CGContextAddEllipseInRect(context, aRect); //椭圆
    CGContextDrawPath(context, kCGPathStroke);
}

6、画圆弧

- (void)drawCircular//弧
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context); //根据图形上下文创建一个空路径
    CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
    CGContextMoveToPoint(context, 100, 250);
    CGContextAddArcToPoint(context, 50, 250, 50, 300, 50);//增加一段弧路径,使用半径和切点做参数
    CGContextStrokePath(context);
}

7、画渐变色

- (void)drawGradient
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClip(context);//根据路径剪裁图片,调用这个函数之前,路径不可以填充或者描边,否则没有剪裁效果
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    CGFloat colors[] =
    {
        204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 0.2,
        29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 0.2,
        0.0 / 255.0, 50.0 / 255.0, 126.0 / 255.0, 0.2,
    };
    CGGradientRef gradient = CGGradientCreateWithColorComponents
    (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
    CGColorSpaceRelease(rgb);
    CGContextDrawLinearGradient(context, gradient,CGPointMake(0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),kCGGradientDrawsBeforeStartLocation);
}

8、画虚线

- (void)drawImaginaryLine
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 20.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGFloat dashArray[] = {2,6,4,2};
    CGContextSetLineDash(context, 3, dashArray, 4);//绘制虚线,跳过3个再画虚线,所以刚开始有6-(3-2)=5个虚点
    CGContextMoveToPoint(context, 10, 300);
    CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);
    CGContextStrokePath(context);
}

相关文章

  • CGContextRef日常使用

    日常更新CGContextRef的使用: 1、获取某个View的截图 2、画一个文本 3、 画一条线 4、 画矩形...

  • 高清大长图拼接

    采取异步线程拼接多个高清大图,使用CoreGraphics绘制,关键api如下: CGContextRef CGB...

  • CGContextRef

    /* CoreGraphics - CGContext.h */ /** Graphics state funct...

  • CGContextRef

    CGContextRef An opaque type that represents a Quartz 2D d...

  • CGContextRef

    以前很不喜欢写技术文章,现在越来越喜欢写了,还是古语说得好,好记性不如烂笔头,当自己总是掉入同一个坑之后,发现记笔...

  • CGContextRef

    自定义显示画框的view 画直线 写文字 画弧线 画圆 有填充颜色的圆 画矩形 画扇形,其实就是画圆,设置一下角度。

  • CGContextRef

    什么是 CGConTextRef CGConTextRef相当于一个画布,我们可以在上面画各种各样的图形,而这个画...

  • CGContextRef

    画纸 设置 画字 画线 画圆 画图

  • Core Graphics初步使用

    view:使用Core Graphics之前需要指定一个用于绘图的图形上下文(CGContextRef),这个图形...

  • Quartz2D学习笔记(一):上下文与路径

    CGContextRef currentRef = UIGraphicsGetCurrentContext(); ...

网友评论

    本文标题:CGContextRef日常使用

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