在苹果中心文档里面可以看到绘图方面的内容和UIView很大关联,UIBezierPath 封装很多有用的方法,使用它可以描点,画贝塞尔曲线。今天看的内容除了涉及到画图 还涉及到绘制位图方面的知识点,很实用。我还没学会怎样使用这个帮助文档,还需要时间练习。在这个文档里面有一些案例可以很方便教授使用。当忘记了可以查询一下。查看绘图相关的API和介绍
//绘图-画线和画一个图,当初不知道为什么要这样操作,模糊地用了一下api也大概实现出来。
下面继承一个UIView类,名为MyView。在drawRect 里面重写这个类,在里面画图。操作点图案也行。
下面记录一下绘制一条线。给它加上点颜色。
#import "MyView.h"
@implementation MyView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
//在这里绘图
UIImage *image =[UIImage imageNamed:@"test.png"];
CGSize size = [image size];
UIGraphicsBeginImageContext(CGSizeMake(size.width*2, size.height));
[image drawAtPoint:CGPointMake(size.width/2, 0)];
UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView =[[UIImageView alloc]initWithImage:im];
[self addSubview:imageView];
//绘图-画线
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 200, 200);
CGContextStrokePath(context);
}
@end

MyView *myview = [[MyView alloc]init];
myview.frame = self.view.frame;
myview.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myview];
[myview setNeedsDisplay];
UIView 默认是黑色,创建的时候,可以恰当给它的改成白色。顺便给它一个宽度小。创建完成后 发送[myview setNeedsDisplay]的消息,调用里面方法。
2015-5-4更新
使用UIBezierPath也可以达到绘制路径的做法。
如果声明了 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 后,不再设置颜色的时候,程序表现出来的结果是使用UIBezierPath 描绘出的线条颜色和上面的程序是一样,但在填充的时候,添加 [[UIColor greenColor] set]; 则可以改变不同的色。
UIBezierPath *p=[[UIBezierPath alloc]init];
[[UIColor greenColor] set];
[p moveToPoint:CGPointMake(30, 30)];
[p addLineToPoint:CGPointMake(30, 200)];
[p setLineWidth:2];
[p stroke];