CoreGraphics详解

来源:互联网 发布:疯狂java讲义 下载 编辑:程序博客网 时间:2024/06/03 15:29

CoreGraphics详解

  • CoreGraphics详解
    • CoreGraphics绘图
      • 综述
      • 绘制一个矩形
      • 绘制一个椭圆
      • 绘制曲线
      • 绘制圆形
      • 链接点来绘制为图形
      • UIView四周描线
      • 绘制图像和文本

CoreGraphics绘图

综述

 描绘系统会调用UIView的drawRect方法,所以coregraphics的所有实现代码放在该函数内,setNeedsDisplay是更新整个视图,setNeedsDisplayInRect是更新视图的一个区域

绘制一个矩形

CGRect rectangle=CGRectMake(0, 0, 200, 200);//获取当前图形CGContextRef ctx=UIGraphicsGetCurrentContext();//当前图形添加矩形CGContextAddRect(ctx, rectangle);//填充颜色CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);//绘制当前区域CGContextFillPath(ctx);

绘制一个椭圆

 CGRect rectangle=CGRectMake(30, 30, 200, 200);//获取当前图形CGContextRef ctx=UIGraphicsGetCurrentContext();//当前图形添加椭圆CGContextAddEllipseInRect(ctx,rectangle);//填充颜色CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);//绘制当前区域CGContextFillPath(ctx);###绘制一个椭圆CGRect rectangle=CGRectMake(30, 30, 200, 200);//获取当前图形CGContextRef ctx=UIGraphicsGetCurrentContext();//当前图形添加椭圆CGContextAddEllipseInRect(ctx,rectangle);//填充颜色CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);//绘制当前区域CGContextFillPath(ctx);

绘制曲线

//get the current drawing contextCGContextRef ctx=UIGraphicsGetCurrentContext();//start drawing with pointCGContextBeginPath(ctx);//draw to this pointCGContextMoveToPoint(ctx, 200.0, 200.0);//draw 贝塞尔曲线CGContextAddQuadCurveToPoint(ctx, 150, 220, 150, 300);//fill color/** *  @brief 在指定点追加二次贝塞尔曲线,通过控制点和结束点指定曲线。 *         关于曲线的点的控制见下图说明,图片来源苹果官方网站。参数按顺序说明 *  @param c   当前图形 *  @param cpx 曲线控制点的x坐标 *  @param cpy 曲线控制点的y坐标 *  @param x   指定点的x坐标值 *  @param y   指定点的y坐标值 * */    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);CGContextSetLineWidth(ctx, 10);//draw this areaCGContextStrokePath(ctx);

绘制圆形

//获取当前图形CGContextRef ctx=UIGraphicsGetCurrentContext();//当前图形添加椭圆CGContextAddArc(ctx, 50.0, 50.0, 25.0,  0, 2 * M_PI, 1);    //填充颜色CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);//绘制当前区域CGContextFillPath(ctx);

链接点来绘制为图形

 CGContextRef ctx=UIGraphicsGetCurrentContext();//start drawing with pointCGContextBeginPath(ctx);//draw to this pointCGContextMoveToPoint(ctx, 100.0, 100.0);CGContextAddLineToPoint(ctx, 100.0, 200.0);CGContextAddLineToPoint(ctx, 200.0, 100.0);//return to orgin pointCGContextClosePath(ctx);//fill colorCGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);//绘制当前区域CGContextFillPath(ctx);

UIView四周描线

-(void)drawRect:(CGRect)rect{[super drawRect:rect];//get the current drawing contextCGContextRef context = UIGraphicsGetCurrentContext();CGContextBeginPath(context);CGContextSetLineWidth(context,1);CGContextMoveToPoint(context, 0, 0);CGContextAddLineToPoint(context,                        CGRectGetWidth(self.bounds),                        0);CGContextMoveToPoint(context,                     CGRectGetWidth(self.bounds),                     0);CGContextAddLineToPoint(context,                        CGRectGetWidth(self.bounds),                        CGRectGetHeight(self.bounds));CGContextMoveToPoint(context,                     CGRectGetWidth(self.bounds),                     CGRectGetHeight(self.bounds));CGContextAddLineToPoint(context,0,CGRectGetHeight(self.bounds));CGContextMoveToPoint(context,0,                CGRectGetHeight(self.bounds));CGContextAddLineToPoint(context,0,0);CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//线条颜色CGContextStrokePath(context);}

绘制图像和文本

-[UIImage drawAtPoint:(CGPoint)point];-[UIImage drawInRect:(CGRect)rect];-[UIImage drawAsPatternInRect:(CGRect)rect];
0 0