iOS最简单绘图实现
来源:互联网 发布:淘宝客服问题总结 编辑:程序博客网 时间:2024/06/05 00:32
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
- iOS最简单绘图实现
- 【iOS】CoreGraphics简单绘图
- iOS 绘图简单三角形
- iOS 实现绘图
- iOS-实现最简单的画线功能
- iOS: 最简单的splash screen实现
- iOS滑动返回的最简单实现
- IOS开发:CoreGraphics简单绘图
- 随笔-iOS学习简单绘图
- iOS---实现在屏幕上实时绘图的简单效果---CAShaperLayer和UIBezierPath的简单运用
- Qt之最简单的绘图程序
- js+canvas实现简单绘图
- 使用BitmapShader实现简单绘图
- 最简单的IOS
- 最简单的IOS
- 最简单的IOS
- 最简单的IOS
- 最简单的IOS
- 【quick-cocos2dx从零开始做游戏】【二】创建工程与运行
- sqlite3使用基础
- hdoj 5586 Sum 【dp】
- Linux笔记分享-crazychao
- VMware虚拟机下安装原版XP外文系统
- iOS最简单绘图实现
- Android自定义相机方法及保存照片方式
- hdu5587 BestCoder Round #64 (div.2)
- 【HDU5583 2015上海赛区L】【找规律 正难则反】LCM Walk 目标状态(x,y)哪些点走公倍数能走到它
- HDU5587
- hdoj 5587 Array 【思维】
- asp.net中使用getElementById的注意点
- NEUQOJ 1495 田鼠看热闹(Ⅰ)(枚举 + 模拟)
- 利用OpenCV找到图片中的乒乓球,返回球的圆心及半径,去除图片中非感兴趣区域