iOS最简单绘图实现

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值