drawView.h:
#import <UIKit/UIKit.h>
@interface drawView : UIView
@property (nonatomic,retain) NSMutableArray *array; //全局变量,存储所有绘图线条
@end
drawView.m:
#import "drawView.h"
@implementation drawView
@synthesize array;
- (id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
array=[NSMutableArray arrayWithCapacity:1];//可变数组也必须先分配1个存储空间,否则无法使用。
self.backgroundColor=[UIColor whiteColor];
UIButton *backButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0,(self.frame.size.width-40)/3, 30)];
UIButton *cleanButton=[[UIButton alloc]initWithFrame:CGRectMake((self.frame.size.width-40)/3+20, 0,(self.frame.size.width-40)/3, 30)];
UIButton *saveButton=[[UIButton alloc]initWithFrame:CGRectMake((self.frame.size.width-40)/3*2+40, 0, (self.frame.size.width-40)/3, 30)];
backButton.backgroundColor=[UIColor redColor];
backButton.tag=1;
[backButton setTitle:@"撤销" forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
cleanButton.backgroundColor=[UIColor redColor];
cleanButton.tag=2;
[cleanButton setTitle:@"清空" forState:UIControlStateNormal];
[cleanButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
saveButton.backgroundColor=[UIColor redColor];
saveButton.tag=3;
[saveButton setTitle:@"保存" forState:UIControlStateNormal];
[saveButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:backButton];
[self addSubview:cleanButton];
[self addSubview:saveButton];
return self;
}
- (void)click:(UIButton *)sender{
if (sender.tag==1) {
[array removeLastObject];
[self setNeedsDisplay];
}
else if(sender.tag==2)
{
[array removeAllObjects];
[self setNeedsDisplay];
}
else if(sender.tag==3)//保存图像
{
UIGraphicsBeginImageContext(self.frame.size);//创建bitmap上下文
[self.layer renderInContext:UIGraphicsGetCurrentContext()];//保存当前的层到bitmap
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();//获取保存后的图像指针
UIGraphicsEndImageContext();//保存图像(至内存)结束
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);//保存图像至相册。
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];//获得触摸对象
CGPoint startPoint=[touch locationInView:self];
UIBezierPath *path=[UIBezierPath bezierPath];
[path moveToPoint:startPoint];//用贝塞尔曲线对象存储一笔的起点,起点需用moveToPiont方法
[array addObject:path];//将贝塞尔曲线对象存储到全局数组中
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch=touches.anyObject;
CGPoint currentPoint=[touch locationInView:self];
UIBezierPath *path=array.lastObject;//从全局数组中取出最后一个贝塞尔曲线对象,肯定是当前在画的起点
[path addLineToPoint:currentPoint];//贝塞尔曲线对象存储接下来的所有触摸过的点,添加剩下的点需用addLineToPoint方法。
[self setNeedsDisplay];//重绘过程调用了drawRect方法。
}
- (void)drawRect:(CGRect)rect
{//设置绘图的上下文
[[UIColor redColor]setStroke];//画笔颜色
for (UIBezierPath *path in array) {
path.lineWidth=3;//画笔宽度
[path stroke];//绘制贝塞尔曲线对象中的两个点(直线)
}
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo //<span style="font-family: Arial, Helvetica, sans-serif;">保存成功与否的官方回调函数</span>
{
if (error==nil) {
NSLog(@"保存成功!");
}
else{
NSLog(@"保存失败!");
}
}
@end