iOS-初解绘画

绘图:需要使用Quartz 2D 


绘画常规步骤:

(1)要有一支画笔 

①设置笔画宽度 CGContextSetLineWidth:

②设置笔画颜色 set

设置笔画的颜色 setStroke(下文有与set的区别)

画线区域范围的填充 setFill

⑤设置画笔填充样式

typedef CF_ENUM (int32_t, CGPathDrawingMode) {
  kCGPathFill,      //只填充
  kCGPathEOFill, //
  kCGPathStroke,//画笔颜色
  kCGPathFillStroke,//既有填充又有画笔
  kCGPathEOFillStroke
};

(2)要有一个画板CGContextRef

(3)画图的内容

(4)把内容添加到上下文(画板)

(5)把内容画到画板上


常用方法介绍(这里没有按画图的步骤介绍方法)

(1)CGContextRef 上下文(画布)

(2)路径(贝塞尔路径)

①UIBezierPath

②通过点绘制一个路径  CGMutablePathRef

③注意 必须设置起始点 CGContextMoveToPoint

(3)画形状

①画矩形:CGContextAddRect:

②画曲线:CGContextAddCurveToPoint

③画圆形:CGContextAddEllipseInRect

.......

(4)截图

①开始获得图片上下文  UIGraphicsBeginImageContext

获得当前图片上下文的图片  UIGraphicsGetImageFromCurrentImageContext() 是从画图视图layer上得到的

③关闭图片上下文  UIGraphicsEndImageContext 

开始截图  UIGraphicsBeginImageContextWithOptions

⑤把路径绘制到上下文  CGContextStrokePath

⑥直接把路径绘制到界面stroke


1、画线

(1)首先得有一张画板 CGContextRef

(2)路径 画图的内容

(3)把路径添加到上下文CGContextAddPath

(4)把路径渲染到上下文CGContextStrokePath


//画直线

- (void)addLine{

//  1、创建画布(上下文)

//    获得当前的上下文当做画布

    CGContextRef context =UIGraphicsGetCurrentContext();

//    2、创建画图的内容

/*

使用path,则一个path就代表一条路径。

比如:如果要在上下文中绘制多个图形,这种情况下建议使用path

*/

    UIBezierPath *path = [UIBezierPathbezierPath];

//    21起始点

    /*

     Point 中心点

     x:中心点x

     y:中心点y

     

     y不变,x从小值到大值:横向直线

     */

    [path moveToPoint:CGPointMake(20,20)];

//    22添加终点

//    CGContextAddLineToPoint(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>)这种方法也可以用下面这个

    

    [path addLineToPoint:CGPointMake(200,200)];

    

    [path addLineToPoint:CGPointMake(100,200)];

    

    

//    2.3 设置画笔宽度

    path.lineWidth =80

    ;

//    24设置画笔颜色

//    set 设置既有填充又有画笔颜色

//    [[UIColor whiteColor] set];

    

//   设置画笔颜色  setStroke设置的是边框的颜色

    [[UIColorwhiteColor ] setStroke];

//    设置填充的颜色

    [[UIColorbrownColor] setFill];

    

//    3、把画的内容(路径)添加到画布(上下文)

    

    CGContextAddPath(context, path.CGPath);

    

//    4渲染内容到上下文

//    CGContextStrokePath(context);

    

//    设置填充的样式既有填充又有画笔颜色

    CGContextDrawPath(context,kCGPathFillStroke);

    

}

//画矩形

- (void)addRect{

    

//    1、画布

    CGContextRef rect =UIGraphicsGetCurrentContext();

    

//    2、内容

    CGContextAddRect(rect,CGRectMake(0,0, 100,100));

    

//    设置画笔颜色

//    [[UIColor redColor]set];

    

    [[UIColorredColor] setFill];

    [[UIColoryellowColor] setStroke];

    

//    设置画笔宽度

    CGContextSetLineWidth(rect,10);

    

//    3、渲染

//    直接渲染矩形

//    CGContextStrokeRect(rect, CGRectMake(10, 10, 100, 100));

    

    CGContextDrawPath(rect,kCGPathFillStroke);

    

}


//画圆形

- (void)addRound{

    

//    1、画布

    contextRef =UIGraphicsGetCurrentContext();

    

//    2、内容

    CGContextAddEllipseInRect(contextRef,CGRectMake(100,100, 100,100));

    

    [[UIColoryellowColor] set];

    

//    3、渲染

    CGContextDrawPath(contextRef,kCGPathFillStroke);

    

}


//画曲线

- (void)addCurve{

//    1、画布

    CGContextRef curve =UIGraphicsGetCurrentContext();

    

//    2、内容

    UIBezierPath *path = [UIBezierPathbezierPath];

    

//    起始点

//    [path moveToPoint:CGPointMake(100, 100)];

    

//    画曲线(直接绘制成曲线)

//    [path addCurveToPoint:CGPointMake(100, 200) controlPoint1:CGPointMake(20, 20) controlPoint2:CGPointMake(300, 300)];

    

    [[UIColorredColor]setStroke];

    [[UIColoryellowColor]setFill];

    

//    画弧

    /*

     center:中心点不用起始点

     radius:半径

     startAngle:开始弧度

     endAngle:结束弧度

     clockwise:是顺时针还是逆时针

     */

    [path addArcWithCenter:CGPointMake(100,100) radius:100startAngle:M_PIendAngle:1/4clockwise:YES];

    

//    把内容添加到画布上

    CGContextAddPath(curve, path.CGPath);

    

//    渲染

    CGContextDrawPath(curve,kCGPathFillStroke);

    

}


//画线简化

- (void)addLineEasy{

//    1、路径

//    2、画出内容

    

//    1

    UIBezierPath *path = [UIBezierPathbezierPath];

    [path moveToPoint:CGPointMake(200,200)];

    [path addLineToPoint:CGPointMake(300,40)];

    [[UIColorwhiteColor]set];

    

//    2

    [path stroke];

    

}


//截屏

- (void)cutScreen{

//    1、获得一个图片的上下文(画布)

//    2、画布的上下文

//    3、设置截图的参数

//    4、截图

//    4、关闭图片的上下文

//    5、保存

    

//    1

    UIGraphicsBeginImageContext(self.frame.size);

//    2

    [selfaddRound];

    

    [self.layerrenderInContext:contextRef];

    

//    3

    /*

     size:图片尺寸

     opaque:是否是不透明  yes是不透明

     scale:比例

     */

    UIGraphicsBeginImageContextWithOptions(self.frame.size,YES, 1);

    

//    4

    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();

    

//    5

    UIGraphicsEndImageContext();

    

//    6

    UIImageWriteToSavedPhotosAlbum(image,self, @selector(image:didFinishSavingWithError:contextInfo:),nil);

    

}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

    

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章