iOS开发

绘画--画板(作业)

2016-07-22  本文已影响0人  金牛忘忧

//

//ViewController.m

//绘画--画板(作业)

//

#import"ViewController.h"

#import"DrawView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interfaceViewController()

{

DrawView*drawView;

UIImage* sendImg;

}

@property(weak,nonatomic)IBOutletUIButton*clearButton;

@property(weak,nonatomic)IBOutletUIButton*undoButton;

@property(weak,nonatomic)IBOutletUIButton*eraserButton;

@property(weak,nonatomic)IBOutletUISlider*widthSlider;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

drawView= [[DrawViewalloc]initWithFrame:CGRectMake(0,70,kScreenWidth,kScreenHeight-190)];

drawView.backgroundColor= [UIColorgrayColor];

[self.viewaddSubview:drawView];

//NSLog(@"%@",NSHomeDirectory());获得路径是使用

}

- (IBAction)clearButtonAction:(UIButton*)sender {

[drawView.linesremoveAllObjects];

[drawViewsetNeedsDisplay];

}

- (IBAction)undoButtonAction:(UIButton*)sender {

[drawView.linesremoveLastObject];

[drawViewsetNeedsDisplay];

}

- (IBAction)eraserButtonAction:(UIButton*)sender {

drawView.color= [UIColorgrayColor];

drawView.lineWidth=15;

}

- (IBAction)saveButtonAction:(UIButton*)sender {

[selfselectView];

//将新图片写到桌面上

NSString*filePath =@"/Users/wuxujian/Desktop/mm.png";

NSData*imgData =UIImageJPEGRepresentation(sendImg,1);

[imgDatawriteToFile:filePathatomically:YES];

//存储到相册

UIImageWriteToSavedPhotosAlbum(sendImg,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);

}

- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo;{

NSLog(@"保存图片成功");

}

//截取图片

-(UIImage*)selectView{

UIGraphicsBeginImageContext(drawView.bounds.size);

[drawView.layerrenderInContext:UIGraphicsGetCurrentContext()];

UIImage*viewImg =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndPDFContext();

CGImageRefimageRef = viewImg.CGImage;

CGRectrect =CGRectMake(drawView.frame.origin.x,drawView.frame.origin.y,drawView.frame.size.width,drawView.frame.size.height);

CGImageRefrefImgRef =CGImageCreateWithImageInRect(imageRef, rect);

sendImg= [[UIImagealloc]initWithCGImage:refImgRef];

returnsendImg;

}

- (IBAction)greenButtonAction:(UIButton*)sender {

drawView.color=[UIColorgreenColor];

}

- (IBAction)redButtonAction:(UIButton*)sender {

drawView.color=[UIColorredColor];

}

- (IBAction)blueButtonAction:(UIButton*)sender {

drawView.color=[UIColorblueColor];

}

- (IBAction)widthSliderAction:(UISlider*)sender {

drawView.lineWidth=_widthSlider.value/_widthSlider.maximumValue*10+1;

}

@end

//LineModel.h

//绘画--画板(作业)

//

#import

#import

#import

@interfaceLineModel :NSObject

@property(assign,nonatomic)CGMutablePathRefpath;//路径

//颜色

@property(strong,nonatomic)UIColor*color;

//宽度

@property(assign,nonatomic)CGFloatwidth;

@end

//LineModel.m

//绘画--画板(作业)

//

#import"LineModel.h"

@implementationLineModel

-(void)setPath:(CGMutablePathRef)path{

if(_path!= path) {

CGPathRelease(_path);

CGPathRetain(path);

_path= path;

}

}

- (void)dealloc {

CGPathRelease(_path);

}

@end

//DrawView.h

//绘画--画板(作业)

//

#import

@interfaceDrawView :UIView

@property(strong,nonatomic)UIColor*color;

@property(assign,nonatomic)CGFloatlineWidth;

//使用一个可变数组,来管理所有的线

@property(strong,nonatomic)NSMutableArray*lines;

@end

//DrawView.m

//绘画--画板(作业)

//

#import"DrawView.h"

#import"LineModel.h"

@interfaceDrawView()

{

CGMutablePathRef_linePath;

LineModel*line;

////使用一个可变数组,来管理所有的线

//NSMutableArray *_lines;

}

@end

@implementationDrawView

- (instancetype)initWithFrame:(CGRect)frame {

self= [superinitWithFrame:frame];

if(self) {

self.backgroundColor= [UIColorwhiteColor];

_lines= [NSMutableArrayarray];

_color= [UIColorblackColor];

_lineWidth=3;

}

returnself;

}

//1记录手指移动的轨迹

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {

//创建路径

_linePath=CGPathCreateMutable();

//设置起始点

UITouch*touch = [touchesanyObject];

CGPointlocation = [touchlocationInView:self];

CGPathMoveToPoint(_linePath,NULL, location.x, location.y);

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event {

//向路径中添加点

UITouch*touch = [touchesanyObject];

CGPointlocation = [touchlocationInView:self];

CGPathAddLineToPoint(_linePath,NULL, location.x, location.y);

[selfsetNeedsDisplay];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event {

//将当前画完的线条加入到数组中去

line= [[LineModelalloc]init];

line.path=_linePath;

line.color=_color;

line.width=_lineWidth;

[_linesaddObject:line];

CGPathRelease(_linePath);

_linePath=NULL;

//绘制线条

[selfsetNeedsDisplay];

}

//2绘制图形界面

- (void)drawRect:(CGRect)rect {

//获取图形上下文

CGContextRefcontext =UIGraphicsGetCurrentContext();

//绘制数组中储存的线条

for(LineModel*lineModelin_lines) {

CGContextSetLineWidth(context, lineModel.width);

[lineModel.colorsetStroke];

//绘制路径

CGContextAddPath(context, lineModel.path);

CGContextDrawPath(context,kCGPathStroke);

}

//全局变量中储存的线条

if(_linePath==NULL) {

return;

}

//绘制路径

CGContextAddPath(context,_linePath);

CGContextDrawPath(context,kCGPathStroke);

}

@end

上一篇 下一篇

猜你喜欢

热点阅读