ios- 绘图

画图--颜色渐变

CGGradientCreateWithColorComponents 方法创建一个渐变,根据提供的颜色成分

CGGradientRef CGGradientCreateWithColorComponents(

   CGColorSpaceRef space,

   const CGFloatcomponents[],

   const CGFloat locations[],

   size_t count

);

CGContextDrawLinearGradient 方法

void CGContextDrawLinearGradient(

   CGContextRef context,

   CGGradientRef gradient,

   CGPoint startPoint,

   CGPoint endPoint,

   CGGradientDrawingOptionsoptions

);

context:绘画上下文

gradient:指定的渐变对象

startPoint:设置绘制渐变的开始坐标

endPoint:设置绘制渐变的结束坐标

options:一个表示,指示是否绘制渐变的范围超出开始坐标,结束坐标;如果传入0,那么绘制将不会超出范围。

- (void)drawRect:(CGRect)rect

{ // 得到当前绘画上下文

   CGContextRef currentContext =UIGraphicsGetCurrentContext();

   //绘画前,保存绘画上下文状态

   CGContextSaveGState(currentContext);

   //获取一个设备相关的颜色空间

   CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

    //定义开始颜色

   UIColor *startColor =[UIColor orangeColor];

    //获得颜色成分

   CGFloat *startColorComponents=(CGFloat *)CGColorGetComponents([startColorCGColor]);

   UIColor *endColor =[UIColor blueColor];

   CGFloat *endColorComponents=(CGFloat *)CGColorGetComponents([endColor CGColor]);

   CGFloatcolorComponents[8] = {       

      startColorComponents[0],

      startColorComponents[1],

      startColorComponents[2],

      startColorComponents[3],


      endColorComponents[0],

      endColorComponents[1],

      endColorComponents[2],

      endColorComponents[3], 

   };

   CGFloatcolorIndices[2] = {0.0f, 1.0f};

    //创建一个渐变

   CGGradientRef gradient =CGGradientCreateWithColorComponents(colorSpace,(const CGFloat *)&colorComponents,(const CGFloat*)&colorIndices,2);

    //释放颜色空间(使retain1

   CGColorSpaceRelease(colorSpace);

   CGPoint startPoint,endPoint;

   startPoint = CGPointMake(120,260);

   endPoint = CGPointMake(200.0f, 260);

   // 绘制指定的渐变填充

   CGContextDrawLinearGradient(currentContext, gradient,startPoint,endPoint,0);

   CGGradientRelease(gradient);

   CGContextRestoreGState(currentContext);

}

space:颜色空间对象

components:定义渐变(gradient)的每一种颜色的颜色成分。一般包括R,G,B,和透明度;如果为RGB颜色空间,那么只包括R,G,B,而没有透明度。

如果,我们使用的颜色空间是一个RGBA颜色空间,并且我们想使用2种颜色的渐变(一个颜色用于开始位置,一个用于结束位置)。那么,我们需要提供8个颜色成分。分别为开始位置的RGBA,和结束位置的RGBA。

locations:每一种颜色颜色成分的位置。位置的范围为0-1之间。

count:提供的位置数量。

CGContextSaveGState(context);

然后我们截取对应的context

CGContextClipToRect(context, clipRect); 

......

用完这个context之后,我们还要恢复到之前的context

CGContextRestoreGState(context);

至此,就完成了。我实现的是在屏幕里画一个矩形,然后在矩形里,实现渐进色的功能,大家可以尝试一下。


图片绘制
将图片直接绘制倒view上

UIImage* image = [UIImage imageNamed:@"5.png"];

[image drawAtPoint:CGPointMake(100, 100)];

//-------------------------------------------------------------------------------------------------------------------------------------------------------
文字绘制:将文字直接绘制倒view上

NSString* str = @"ABCDEFG";

[str drawInRect:CGRectMake(100, 100, 200, 50) withFont:[UIFontsystemFontOfSize:30.0]   lineBreakMode:UILineBreakModeCharacterWrapalignment:UITextAlignmentLeft];

换行方式                                                               对其方式

//-------------------------------------------------------------------------------------------------------------------------------------------------------

绘制线

   //得到上下文

    CGContextRef ref =UIGraphicsGetCurrentContext();

    //线宽设定

    CGContextSetLineWidth(ref,10.0);

    //线的边角样式(圆角型)

    CGContextSetLineCap(ref,kCGLineCapRound);

    CGContextSetLineJoin(ref,kCGLineJoinRound);


   //虚线

    float lengths[] ={20,10};

   //(上下文,起点的偏移量,事例描述的时20像素的虚线10的空格,数组有2个元素)

    CGContextSetLineDash(ref,0, lengths, 2);

    //如果要恢复线的属性只需要输入null即可

    CGContextSetLineDash(ref,0, NULL, 0);

   //线条颜色

   CGContextSetStrokeColorWithColor(ref, [UIColorpurpleColor].CGColor);


    //移动绘图点

    CGContextMoveToPoint(ref,100, 100);

    //绘制直线

   CGContextAddLineToPoint(ref, 220, 220);

   CGContextAddLineToPoint(ref, 100, 220);

   //封闭(不需要绘制第三条线,运行封闭语句系统自动完成图形的绘制)

   CGContextClosePath(ref);

    //开始绘制线并在view上显示

   CGContextStrokePath(ref);

//-------------------------------------------------------------------------------------------------------------------------------------------------------

绘制图:

    //得到上下文

    CGContextRef ref =UIGraphicsGetCurrentContext();

   //线宽设定

    CGContextSetLineWidth(ref,5.0);

   //填充的颜色

   CGContextSetFillColorWithColor(ref, [UIColorpurpleColor].CGColor);

   //线条颜色

   CGContextSetStrokeColorWithColor(ref, [UIColorblueColor].CGColor);

   //透明度

    CGContextSetAlpha(ref,0.5);

   //影子的偏移量,颜色

   CGContextSetShadowWithColor(ref,CGSizeMake(20, 20), 20, [UIColor grayColor].CGColor);

   //绘制圆型

    CGContextAddRect(ref,CGRectMake(100, 100, 100, 100));

   //图形显示方式为:线条填充图一起显示(此方法一定要在绘制图形完成后使用,否则无效

    CGContextDrawPath(ref,kCGPathFillStroke);

   CGContextStrokePath(ref);

//-------------------------------------------------------------------------------------------------------------------------------------------------------

绘制半圆:

   CGContextRefref = UIGraphicsGetCurrentContext();

   CGContextSetFillColorWithColor(ref,[UIColorredColor].CGColor);

   //如果没有这句,那么下面绘制就是一个从0度到100度的圆型的一部分,因为没有设置move点

   CGContextMoveToPoint(ref,150,150);

   //(100为半径,最后的0说明时顺时针[逆时针为1])

   CGContextAddArc(ref, 150,150,100,0 *M_PI / 180, 100 *M_PI /180,0);

   CGContextFillPath(ref);

//---------------------------------

   CGContextSetFillColorWithColor(ref,[UIColorgreenColor].CGColor);

   CGContextMoveToPoint(ref,150,150);

   CGContextAddArc(ref, 150,150,100,100 *M_PI / 180, 230 *M_PI /180,0);

   CGContextFillPath(ref);

   CGContextSetFillColorWithColor(ref,[UIColorblueColor].CGColor);

   CGContextMoveToPoint(ref,150,150);

   CGContextAddArc(ref, 150,150,100,230 *M_PI / 180, 360 *M_PI /180,0);

   CGContextFillPath(ref);

//-------------------------------------------------------------------------------------------------------------------------------------------------------

绘制弧线:


   CGContextMoveToPoint(ref,100,350);

   //(120,250)是弧线顶点的坐标(300,350)是弧线最右端的坐标

   CGContextAddQuadCurveToPoint(ref,120,250,300,350);

   CGContextStrokePath(ref);


画正方形边框 

CGContextSetRGBStrokeColor(context, 1, 1.0, 1.0, 1.0);

 CGContextSetLineWidth(context, 2.0); 

CGContextAddRect(context, CGRectMake(2, 2, 270, 270)); 

CGContextStrokePath(context);  

 

 画方形背景颜色 

CGContextTranslateCTM(ctx, 0.0f, self.view.bounds.size.height); 

CGContextScaleCTM(ctx, 1.0f, -1.0f); 

UIGraphicsPushContext(ctx); 

CGContextSetLineWidth(ctx,320); 

CGContextSetRGBStrokeColor(ctx, 250.0/255, 250.0/255, 210.0/255, 1.0); 

CGContextStrokeRect(ctx, CGRectMake(0, 0, 320, 460));

UIGraphicsPopContext();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值