绘制过程:
创建路径 -CGMutablePathRef CGPathCreateMutable (void); //CGMutablePathRef path = CGPathCreateMutable();
设置矩形 -CGRect CGRectMake (CGFloat x,CGFloat y,CGFloat width,CGFloat height);
将所要绘制的图形(矩形)加入路径
加入单个矩形 -void CGPathAddRect (CGMutablePathRef path,const CGAffineTransform *m,CGRect rect); //第二个参数:NULL
加入多个矩形 -void CGPathAddRects (CGMutablePathRef path,const CGAffineTransform *m,const CGRect rects[],size_t count);
创建上下文环境 -CGContextRef UIGraphicsGetCurrentContext (void); //CGContextRef currentContext = UIGraphicsGetCurrentContext();
在环境中添加路径 - void CGContextAddPath (CGContextRef context,CGPathRef path);设置相关属性 - (void)setFill //填充颜色
- (void)setStroke //边界颜色
- void CGContextSetLineWidth (CGContextRef c,CGFloat width); //线宽
绘制路径 -void CGContextDrawPath (CGContextRef c,CGPathDrawingMode mode);
释放 -void CGPathRelease (CGPathRef path);
/* Create the path */
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectangle = CGRectMake(10.0f,30.0f,200.0f,300.0f);
CGPathAddRect(path, NULL,rectangle);
// CGRect rectangle1 = CGRectMake(10.0f,30.0f,200.0f,300.0f);
// CGRect rectangle2 = CGRectMake(40.0f,100.0f,90.0f,300.0f);
// CGRect rectangles[2] = {rectangle1, rectangle2};
// CGPathAddRects(path,NULL,(const CGRect *)&rectangles,2);
/* Get the handle to the current context */
CGContextRef currentContext = UIGraphicsGetCurrentContext();
/* Add the path to the context */
CGContextAddPath(currentContext, path);
/* Set the fill color to cornflower blue */
[[UIColor colorWithRed:0.20f
green:0.60f
blue:0.80f
alpha:1.0f]setFill];
/* Set the stroke color to brown */
[[UIColor brownColor]setStroke];
/* Set the line width (for the stroke) to 5 */
CGContextSetLineWidth(currentContext,5.0f);
/* Stroke and fill the path on the context */
CGContextDrawPath(currentContext,kCGPathFillStroke);
/* Dispose of the path */
CGPathRelease(path);
}